WScript.Shell和阻塞执行?

Fly*_*wat 8 shell vbscript wsh

我使用WScript自动执行某些任务,使用WScript.Shell调用外部程序.

但是,现在它不会等待外部程序完成,而是继续前进.这会导致问题,因为我有一些任务依赖于其他完成的任务.

我使用的代码如下:

ZipCommand = "7za.exe a -r -y " & ZipDest & BuildLabel & ".zip " & buildSourceDir

Set wshShell = WScript.CreateObject("Wscript.Shell")
wshShell.run ZipCommand
Run Code Online (Sandbox Code Playgroud)

有没有办法这样做,所以它阻塞,直到shell执行程序返回?

Fly*_*wat 14

事实证明,虽然循环是严重的CPU占用:P

我找到了一个更好的方法:

ZipCommand = "7za.exe a -r -y " & ZipDest & BuildLabel & ".zip " & buildSourceDir

Set wshShell = WScript.CreateObject("Wscript.Shell")

wshShell.Run ZipCommand,1,1
Run Code Online (Sandbox Code Playgroud)

最后两个参数是Show window和Block Execution :)


Guy*_*uck 7

如果使用"Exec"方法,它将返回一个引用,因此您可以轮询"Status"属性以确定它何时完成.以下是msdn的示例:

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec(ZipCommand)

Do While oExec.Status = 0
    WScript.Sleep 100
Loop
Run Code Online (Sandbox Code Playgroud)

  • 使用Exec()而不是Run()的一个优点是您也可以访问StdOut和StdErr.但是,如果你不关心它,Run()会更简单. (3认同)