Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for executable to finish running

Can you run two executables in a batch file and wait for the first process to end before starting the next?

like image 884
Pillblast Avatar asked Mar 21 '11 09:03

Pillblast


People also ask

How do you wait for an EXE to complete in batch file?

Use /WAIT to Wait for a Command to Finish Execution When we start a program in a Batch file using the START command, we can wait until the program is finished by adding /wait to the START command.

How do you wait in cmd script?

You can use timeout command to wait for command prompt or batch script for the specified amount of time. The time is defined in Seconds. The above commands will break the timeout process on pressing any key. You can use /NOBREAK ignore key presses and wait for the specified time.

How do you tell batch file to wait?

You can insert the pause command before a section of the batch file that you might not want to process. When pause suspends processing of the batch program, you can press CTRL+C and then press Y to stop the batch program.

How do I pause EXE?

Execution of a batch script can also be paused by pressing CTRL-S (or the Pause|Break key) on the keyboard, this also works for pausing a single command such as a long DIR /s listing.


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

In addition, use NT CMD rather than BAT (myscript.cmd).

In response to the comments, brackets removed from the above script around %ERRORLEVEL%. The following seems to behave as expected:

:NOTEPAD
start /wait notepad.exe || goto QUIT

:NEXTITEM1
start /wait mplayer2.exe || goto QUIT

:NEXTITEM2
REM You get the idea...

:QUIT
exit  

The statement after the double-pipe only executes if what is before it fails.

like image 199
nimizen Avatar answered Sep 18 '22 18:09

nimizen


one shorter way:

notepad.exe|more

even this can be used:

notepad|rem

though eventually with more you'll able to catch some output if there's any.And this is the reason it works - the piped command waits for input until the .exe is finished.

like image 24
npocmaka Avatar answered Sep 17 '22 18:09

npocmaka