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 notepad
ECHO Back from notepad
START /W wordpad
ECHO Back from wordpad
START /W notepad
Try something like this...
@ECHO OFF
PSKILL NOTEPAD
START "" "C:\Program Files\Windows NT\Accessories\wordpad.exe"
:LOOP
PSLIST wordpad >nul 2>&1
IF ERRORLEVEL 1 (
GOTO CONTINUE
) ELSE (
ECHO Wordpad is still running
TIMEOUT /T 5
GOTO LOOP
)
:CONTINUE
NOTEPAD
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 OFF
TASKKILL NOTEPAD
START "" "C:\Program Files\Windows NT\Accessories\wordpad.exe"
:LOOP
tasklist | find /i "WORDPAD" >nul 2>&1
IF ERRORLEVEL 1 (
GOTO CONTINUE
) ELSE (
ECHO Wordpad is still running
Timeout /T 5 /Nobreak
GOTO LOOP
)
:CONTINUE
NOTEPAD