gpt4 book ai didi

vbscript - WScript.Shell.Exec-从标准输出读取输出

转载 作者:行者123 更新时间:2023-12-04 13:38:13 26 4
gpt4 key购买 nike

我的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

但是它不会打印任何结果。如何捕获 StdOutStdErr

最佳答案

WScript.Shell.Exec()立即返回 ,即使它启动的过程没有。如果您尝试立即阅读StatusStdOut,那么那里什么也没有。

MSDN documentation建议使用以下循环:

Do While oExec.Status = 0
WScript.Sleep 100
Loop

这会每100毫秒检查一次 Status,直到它更改为止。本质上,您必须等待该过程完成,然后才能读取输出。

只需对代码进行一些小的更改,即可正常工作:
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

关于vbscript - WScript.Shell.Exec-从标准输出读取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32492879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com