I am using the Shell command to start a batch file; however I want to wait for the process to complete before moving on to the next line of code. How can I do this?
Printable View
I am using the Shell command to start a batch file; however I want to wait for the process to complete before moving on to the next line of code. How can I do this?
Find the Batch process, and wait till it stops, or make the batch file output a file (echo Finished > %windir%\temp\finished.txt) and then delete that file
Can you show me how? Does it have something to do w/ the task number returned by the Shell function? I don't want to output a file, because the program could execute concurrently on multiple machines (it is a software audit program that runs on multiple machines on our network). Thank you!
put this code in a bas module;
and use this to run the bat fileCode:
Const INFINITE = -1&
Const SYNCHRONIZE = &H100000
Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Code:dim lngtask as long
dim phandle as long
dim ret as long
itask = Shell("mybat.bat")
pHandle = OpenProcess(SYNCHRONIZE, False, itask)
ret = WaitForSingleObject(pHandle, INFINITE)
ret = CloseHandle(pHandle)
Thank you both very much for the *quick* replies!!!