WScript.Shell和阻止执行?

时间:2020-03-05 18:49:34  来源:igfitidea点击:

我正在使用WScript通过使用WScript.Shell调用外部程序来自动执行某些任务。

但是,现在它不等待外部程序完成,而是继续运行。这引起了问题,因为我有一些任务要先完成,而其他任务则要先完成。

我正在使用类似的代码:

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

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

有没有办法做到这一点,使其阻塞,直到shell执行的程序返回为止?

解决方案

回答

如果使用" 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

回答

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

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

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

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

wshShell.Run ZipCommand,1,1

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