2008-09-08 86 views
6

我使用WScript通過使用WScript.Shell調用外部程序來自動執行一些任務。WScript.Shell並阻止執行?

但是,現在它不會等待外部程序完成,而是繼續前進。這會導致問題,因爲我有一些任務依賴於其他人先完成。

我使用這樣的代碼:

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

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

有沒有辦法做到這一點,因此阻塞,直到shell中執行程序的回報?

回答

5

如果使用「Exec」方法,它會返回一個引用,因此您可以輪詢「狀態」屬性以確定它何時完成。下面是msdn樣本:

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

Set oExec = WshShell.Exec(ZipCommand) 

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

使用Exec()而不是Run()的優點是,您也可以訪問StdOut&StdErr。如果你不關心這一點,但是,Run()更簡單。 – 2009-12-15 19:24:22

13

事實證明,雖然環是嚴重的CPU豬:P

我發現了一個更好的辦法:

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

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

wshShell.Run ZipCommand,1,1 

最後兩個參數是顯示窗口和塊執行:)