How to wait for a process to terminate to execute another process in batch file
Use start /w programname to wait for the end of programname
START /W notepadECHO Back from notepad START /W wordpadECHO Back from wordpadSTART /W notepad
Try something like this...
@ECHO OFFPSKILL NOTEPADSTART "" "C:\Program Files\Windows NT\Accessories\wordpad.exe":LOOPPSLIST wordpad >nul 2>&1IF ERRORLEVEL 1 ( GOTO CONTINUE) ELSE ( ECHO Wordpad is still running TIMEOUT /T 5 GOTO LOOP):CONTINUENOTEPAD
I used PSLIST
and PSEXEC
, but you could also use TASKKILL
and TASKLIST
. The >nul 2>&1
is just there to hide all the output from PSLIST
. The SLEEP 5
line is not required, but is just there to restrict how often you check if WordPad is still running.
This is an updated version of aphoria's Answer.
I Replaced PSLIST and PSEXEC with TASKKILL and TASKLIST`. As they seem to work better, I couldn't get PSLIST to run in Windows 7.
Also replaced Sleep with TIMEOUT.
This Was everything i needed to get the script running well, and all the additions was provided by the great guys who posted the comments.
Also if there is a delay before the .exe starts it might be worth inserting a Timeout before the :loop.
@ECHO OFFTASKKILL NOTEPADSTART "" "C:\Program Files\Windows NT\Accessories\wordpad.exe":LOOPtasklist | find /i "WORDPAD" >nul 2>&1IF ERRORLEVEL 1 ( GOTO CONTINUE) ELSE ( ECHO Wordpad is still running Timeout /T 5 /Nobreak GOTO LOOP):CONTINUENOTEPAD