下面是一些 JSL,用於通過 JMP 11 的 RunProgram 函數操作 FTP。 下面是關於 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
謝謝jara95在這個帖子對於 FTP 選項。 沒有它們,FTP 根本無法運行。
RunProgram 是一個 JSL 函數,用於控制計算機上的命令行界面程序。 FTP 使用命令行界面,而 MS-Paint 不使用。 命令行程序有兩個可選的數據流:命令輸入流和結果輸出流。 RunProgram 讓 JMP 將命令寫入外部程序的輸入流 (stdin) 並從外部程序的輸出流 (stdout) 讀取輸出。
RunProgram 在 Help->Scripting Index 中有描述和一些例子。
外部程序從 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 字節數據 Pinging [::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 函數並使它們協調。 當 stdout 上有可用數據或 stdin 可以接受更多數據時,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 的很多用途。