views:

1604

answers:

2

I'm using WScript to automate some tasks, by using WScript.Shell to call external programs.

However, right now it does not wait for the external program to finish, and instead moves on. This causes issues because I have some tasks dependent on others finishing first.

I am using code like:

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

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

Is there a way to do this so it blocks until the shell executed program returns?

+3  A: 

If you use the "Exec" method, it returns a reference, so you can poll the "Status" property to determine when it is complete. Here is a sample from msdn:

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

Set oExec = WshShell.Exec(ZipCommand)

Do While oExec.Status = 0
    WScript.Sleep 100
Loop
Guy Starbuck
Matt Dillard
+5  A: 

Turns out, that while loop is severe CPU hog :P

I found a better way:

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

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

wshShell.Run ZipCommand,1,1

The last two arguments are Show window and Block Execution :)

FlySwat