0

i'm trying to convert this function to wait notepad.exe to a generic wait function (that can wait for any process). I can't add a variable in the sQuery without breaking the syntax, is there any way to bypass the problem ?

Function wait()
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='notepad.exe'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Do While iniproc = 1
    wscript.sleep 5000
    set svc=getobject("winmgmts:root\cimv2")
    sQuery="select * from win32_process where name='notepad.exe'"
    set cproc=svc.execquery(sQuery)
    iniproc=cproc.count
Loop    
Set cproc=nothing
Set svc=Nothing
End Function

Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set wscr = CreateObject("Wscript.shell")
wscr.Run "notepad.exe"

wait()

MsgBox "done!"

I'm going crazy!!

3 Answers 3

1

The WScript.Shell object's Run method, which you're using to start Notepad, has a built-in mechanism to wait for the process to finish. You don't need the WMI-based wait() function in your script at all.

wscr.Run "notepad.exe", 1, True

The True value at the end of the line is what indicates that Run() should wait for the process to finish.

But to answer your specific question, add a parameter to the wait function. I called it ProcessName. Then use this variable instead of notepad.exe. When you call wait(), you specify the name of the process as an argument.

Function wait(ProcessName)
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & ProcessName & "'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
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    
Set cproc=nothing
Set svc=Nothing
End Function

Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set wscr = CreateObject("Wscript.shell")
wscr.Run "notepad.exe"

wait("notepad.exe")

MsgBox "done!"
Sign up to request clarification or add additional context in comments.

Comments

1

Answered by Patrick S. However, there are two superabundant lines inside the Do While loop and two unnecessary lines:

Function wait(sPName)
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='" & sPName & "'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Do While iniproc > 0
    wscript.sleep 5000
    'superabundant' set svc=getobject("winmgmts:root\cimv2")
    'superabundant' sQuery="select * from win32_process where name='notepad.exe'"
    set cproc=svc.execquery(sQuery)
    iniproc=cproc.count
Loop    
'unnecessary' Set cproc=nothing
'unnecessary' Set svc=Nothing
End Function

Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set wscr = CreateObject("Wscript.shell")

sProcessName="calc.exe"
wscr.Run sProcessName
wait( sProcessName)
MsgBox "done " & sProcessName

wscr.Run "notepad.exe"
wait( "notepad.exe")
MsgBox "done notepad.exe"

Comments

0
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_ProcessTrace")
Do
    Set objReceivedEvent = objEvents.NextEvent
    If objReceivedEvent.ProcessName = "svchost.exe" then msgbox objReceivedEvent.ProcessName
'   msgbox objReceivedEvent.ProcessName
Loop

There is Win32_ProcessTrace (starts and stops), Win32_ProcessTraceStart, and Win32_ProcessTraceStop. The script stops and waits for events to occur.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.