Wscript.shell.run和Wscript.shell.exec不显示相同的输出

Wscript.shell.run and Wscript.shell.exec do not display the same outputs

本文关键字:Wscript shell 输出 run exec 显示      更新时间:2023-09-26

我在Javascript中使用ActiveXObject。

var shell = new ActiveXObject("WScript.Shell");

exec = shell.exec('cmd /c ftp -i -A -s:file.ftp host);

var output = exec.StdOut.ReadAll();

我得到预期的错误"无法创建文件",因为该文件已存在于服务器上。这里一切都很好。但是输出不会显示ftp错误代码,而Run方法会显示(553无法创建文件)。

我不使用Run方法,因为唯一可能的输出是将输出重定向到客户端机器上的文件中。

相信我,我读了很多网站(包括Windows官方对Run、Exec的描述)

如何使用WScript.Shell.Exec命令获取ftp命令的错误代码?

更多信息:

exec.StdOut.ReadAll()输出->

"bin
cd my_dir/
mput file_path file_path
Could not create file.
Could not create file.
quit"

WScript.shell.run->的输出文件

ftp> bin
200 Switching to Binary mode.
ftp> cd my_dir/
250 Directory successfully changed.
ftp> mput file_path file_path
200 PORT command successful. Consider using PASV.
553 Could not create file.
200 PORT command successful. Consider using PASV.
553 Could not create file.
ftp> quit
221 Goodbye.

好的,我在一篇8年前的帖子中找到了解决方案。(http://computer-programming-forum.com/61-wsh/813f07658378176c.htm)

为了使用Exec方法从ftp命令中获得完整的输出,必须在ftp命令中添加-v选项。工作起来很有魅力。

var shell = new ActiveXObject("WScript.Shell");
exec = shell.exec(cmd /c ftp -i -v -A -s:file.ftp host);
var output = exec.StdOut.ReadAll();