【发布时间】:2014-07-31 16:09:47
【问题描述】:
我正在使用 cscript.exe 运行我的 JScript 文件。 在脚本中,我需要调用外部 console 命令并获取输出。
试过了:
var oShell = WScript.CreateObject("WScript.Shell");
var oExec = oShell.Exec('cmd /c dir');
WScript.Echo("Status "+oExec.Status);
WScript.Echo("ProcessID "+oExec.ProcessID);
WScript.Echo("ExitCode "+oExec.ExitCode);
和
var oShell = WScript.CreateObject("WScript.Shell");
var ret = oShell.Run('cmd /c dir', 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);
WScript.Echo("ret " + ret);
但没有运气:命令运行(很可能)没有错误,但我没有输出。 请注意这里的 'cmd /c dir' 只是示例,以确保我得到任何输出。
那么,我该怎么做呢?
更新: 我尝试将此https://stackoverflow.com/a/6073170/1013183 转换为 JScript,但也没有运气:
var oShell = WScript.CreateObject("WScript.Shell");
var oExec = oShell.Exec('cmd /c dir');
var strOutput = oExec.StdOut.ReadAll;
WScript.Echo("StdOut "+strOutput);
var strOutput = oExec.StdErr.ReadAll;
WScript.Echo("StdErr "+strOutput);
错误是Microsoft JScript runtime error: Object doesn't support this property or methodvar strOutput = oExec.StdOut.ReadAll; 行
【问题讨论】:
-
@Dariusz 我已经更新了这个问题。仍然不适合我。