Wait for a Process to Finish with VBScript

Sometimes the .exe process exit directly after launched. In this case, it’s necessary to wait until the process .exe not be there anymore to validate that the command is executed correctly.

Here is a possibility to wait for a process to finish before continuing using VBScript.

In the following example, we wait the process notepad.exe does not exist anymore.

Const PROCESSNAME = "notepad.exe"

Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & PROCESSNAME & "'"

Set cproc=svc.execquery(sQuery)
iniproc=cproc.count

If iniproc=0 Then
    MsgBox("The process " & PROCESSNAME & " is inexistent. Operation aborted.")
    Set cproc=Nothing
    Set svc=Nothing
    wscript.quit(1)
End If

Do While iniproc = 1
    wscript.sleep 5000
    Set svc=getobject("winmgmts:root\cimv2")
    sQuery="select * from win32_process where name='" & PROCESSNAME & "'"
    Set cproc=svc.execquery(sQuery)
    iniproc=cproc.count
Loop

MsgBox("All processes of " & PROCESSNAME & " are terminated.")

Set cproc=Nothing
Set svc=Nothing