WScript.Shell and blocking execution?
Asked Answered
L

2

8

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?

Lipoprotein answered 8/9, 2008 at 18:36 Comment(0)
L
15

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 :)

Lipoprotein answered 8/9, 2008 at 19:14 Comment(0)
Q
7

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
Quizmaster answered 8/9, 2008 at 18:44 Comment(1)
An advantage of using Exec() instead of Run() is that you can access StdOut & StdErr as well. If you don't care about that, though, Run() is simpler.Infeasible

© 2022 - 2024 — McMap. All rights reserved.