Wait for the executable to exit

Can I run two executable files in a batch file and wait for the first process to complete before the next one begins?

+7
source share
2 answers

Use start / wait:

:NOTEPAD start /wait notepad.exe IF %ERRORLEVEL% == 0 goto NEXTITEM1 else goto QUIT :NEXTITEM1 start /wait mplayer.exe IF %ERRORLEVEL% == 0 goto NEXTITEM2 else goto QUIT :NEXTITEM2 start /wait explorer.exe IF %ERRORLEVEL% == 0 goto NEXTITEM3 else goto QUIT :NEXTITEM3 REM You get the idea... :QUIT exit 

Also, use NT CMD, not BAT (myscript.cmd).

In response to comments, parentheses are removed from the above script around% ERRORLEVEL%. The following looks as expected:

 :NOTEPAD start /wait notepad.exe || goto QUIT :NEXTITEM1 start /wait mplayer2.exe || goto QUIT :NEXTITEM2 REM You get the idea... :QUIT exit 

A statement after two-pipe execution is executed only if before it completed with an error.

+17
source

one shorter way:

 notepad.exe|more 

even this can be used:

 notepad|rem 

although in the end with more you can catch some conclusion, if any. And this is the reason this works - the piped command waits for input until the .exe is finished.

+2
source

All Articles