以下是一些透過 JMP 11 的 RunProgram 函數操作 FTP 的 JSL。 以下詳細介紹了 RunProgram。
// this example is specific to Windows because of FTP and RunProgram issues
// you'll need to supply a proper site to open and replace
// anonymous... with proper credentials.
// for the example, build a "junk" directory in the temp folder
temp = Convert File Path( "$TEMP", windows ) || "junk"; // no trail back slash
Create Directory( temp ); // typically: "C:\Users\yourname\AppData\Local\Temp\junk"
// use JMP 11 RunProgram to run ftp; supply a ReadFunction to send all
// results to the JMP log
x = RunProgram(
Executable( "ftp" ),
Options( "-v -n" ),
ReadFunction( Function( {this}, Write( this << read ) ) )
);
x << Write( "open ftp.example.com\!n" ); // site to open
x << Write( "user anonymous YOU@yourcompany.com\!n" ); // credentials
x << Write( "dir\!n" ); // look in the log for the file listing
x << Write( "lcd " || temp || "\!n" ); // local change directory...no trail back slash!
x << Write( "get README\!n" ); // fetch file 1
x << Write( "get index.html\!n" ); // and file 2
x << Write( "disconnect\!n" ); // or just bye if nothing else to do
x << Write( "bye\!n" ); // run program terminates when ftp terminates
// when ftp signals end-of-file for reading, RunProgram must
// really be finished. Wait for it...
While( !(x << isReadEof), Wait( .01 ) );
// display the two files in the temp directory
Write( Load Text File( temp || "\!\README" ) );
Write( "\!n\!n\!n" );
Write( Load Text File( temp || "\!\index.html" ) );
Delete Directory( temp ); // clean up
謝謝佳拉95在這個帖子對於 FTP 選項。 沒有它們,FTP 根本無法運作。
RunProgram 是一個 JSL 函數,用於控制電腦上的命令列介面程式。 FTP 使用命令列介面,而 MS-Paint 則不使用。 命令列程式有兩個可選的資料流:命令輸入流和結果輸出流。 RunProgram 讓 JMP 將指令寫入外部程式的輸入流 (stdin) 並從外部程式的輸出流 (stdout) 讀取輸出。
RunProgram 在「幫助」->「腳本索引」中有描述和一些範例。
外部程式與 JMP 異步運行。 上面,變數X是個處理到外部程式;不要覆蓋X直到你完成它,並確保將 RunProgram 回傳值儲存在一個變數中,例如X所以 RunProgram 不會立即遺失。
除非您使用簡單的 RunProgram 介面。 簡單介面同步運行,這表示 RunProgram 在外部程式完成之前不會回到 JMP。 傳回值是外部程式的所有輸出。 以下是一個應該在您的 Windows 電腦上運行的範例(請耐心等待,對您的電腦執行 5 次 ping 操作需要 5 秒鐘):
RP = RunProgram(
Executable("PING.EXE"),
Options( {"-n 5", "localhost"} ),ReadFunction("text"));
show(RP);
輸出看起來像這樣
使用 32 位元組資料 Ping <您的電腦位址> [::1]:
來自::1的回覆:時間<1ms
來自::1的回覆:時間<1ms
來自::1的回覆:時間<1ms
來自::1的回覆:時間<1ms
來自::1的回覆:時間<1ms
::1 的 Ping 統計:
資料包:發送 = 5,接收 = 5,遺失 = 0(0% 遺失),
大約往返時間(以毫秒為單位):
最小值 = 0ms,最大值 = 0ms,平均值 = 0ms
ReadFunction("text") 啟用此行為;它對於在沒有大量監督的情況下運行完成的程序很有用。
FTP 程式碼可能想要大量的監督。 在決定下一個命令應該是什麼之前,您可能需要檢查命令的輸出。 如果必須的話,您可以使用 RunProgram 來完成此操作,但它會很複雜且難以解釋,很難讓下一個人維護。 有一個 WriteFunction,與 ReadFunction 非常相似。 您必須編寫用於讀取和寫入的 JSL 函數並使它們協調。 當標準輸出上有可用資料或標準輸入可以接受更多資料時,RunProgram 將呼叫它們。 腳本索引中的範例之一包含透過 WriteFunction 發送到 CMD.EXE 的命令列表,一次一個。 但它並沒有真正研究 ReadFunction 中的標準輸出資料......只是列印它。
編輯2017年9月30日:修復格式
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />(透過電子郵件解決...) 我沒有指出需要打開 JSL 日誌視窗才能查看結果(並在出現問題時進行偵錯)。日誌視窗預設關閉。查看 -> 日誌 (windows) 或視窗 -> 日誌 (mac)。
特別是,輸出不會出現在 CMD 視窗(黑色 dos-box 視窗)中。除非你執行的程式是cmd.exe。
謝謝!
這看起來很棒,但是我在讓 ftp 或您的 ping 範例通過 JMP 運行時遇到了一些問題。
透過 runprogram 開啟像 Internet Explorer 這樣的程式運作正常,但 ping、ftp 甚至指向「C:\Windows\System32\ftp.exe」都顯示沒有任何操作發生。
任何想法都會非常感激,因為這看起來比 jan95 的範例更具可擴展性
非常感謝您發布此內容。這正是我需要知道的。我可以看到 RunProgram 有很多用途。