我的 VBScript 不显示我执行的任何命令的结果。我知道命令被执行,但我想捕获结果。
我已经测试了多种方法来执行此操作,例如以下方法:
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Select Case WshShellExec.Status
Case WshFinished
strOutput = WshShellExec.StdOut.ReadAll
Case WshFailed
strOutput = WshShellExec.StdErr.ReadAll
End Select
WScript.StdOut.Write strOutput 'write results to the command line
WScript.Echo strOutput 'write results to default output
但它不打印任何结果。我如何捕捉StdOut
and StdErr
?
WScript.Shell.Exec()
回报立即地,即使它启动的进程没有。如果你尝试阅读Status
or StdOut
马上,那里就不会有任何东西了。
The MSDN 文档 https://msdn.microsoft.com/en-us/library/ateytk4a(v=vs.84).aspx建议使用以下循环:
Do While oExec.Status = 0
WScript.Sleep 100
Loop
这检查Status
每 100 毫秒一次,直到发生变化。本质上,您必须等到该过程完成,然后才能读取输出。
通过对代码进行一些小的更改,它就可以正常工作:
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Do While WshShellExec.Status = WshRunning
WScript.Sleep 100
Loop
Select Case WshShellExec.Status
Case WshFinished
strOutput = WshShellExec.StdOut.ReadAll()
Case WshFailed
strOutput = WshShellExec.StdErr.ReadAll()
End Select
WScript.StdOut.Write(strOutput) 'write results to the command line
WScript.Echo(strOutput) 'write results to default output
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)