下面是一些通过 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 有很多用途。