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

393,842

Solution 1

Try running

START /WAIT Install.exe

Solution 2

One shorter way:

Install.exe|more

Also

install|rem

could be used , though with more eventually you'll be able to catch some console output. And this is the reason it works - the piped command waits for input until the .exe is finished.

Update

For windows 8 and 10 there's a new tool coming with windows called scriptrunner which also can be used for this purpose:

ScriptRunner.exe -appvscript Install.exe -appvscriptrunnerparameters -wait

Solution 3

Either calling the exe directly from the batch file, or using start /wait will work but there is a caveat.

If the exe you call then creates other process, such as calling another exe, and then exits the batch file will continue processing after the called exe has terminated, as it has no knowledge of other processes started by it.

In your case this is a real problem because installers normally extract files from some form of compressed container, which may be embedded in the exe itself, then fire off one of the extracted files and exit. Some installers provide command line parameters which tell the original exe not to exit until the entire installation is complete, so that's something you may want to investigate. Other than that, there's no real way around this with batch files alone and would take a programmatic solution to solve.

Solution 4

Here is an example using MATLAB! I have assumed that the path setup for MATLAB is done and MATLAB exit is being ensured by the FileName.m file (or user has specified it internally).

echo off
matlab -nosplash /r "FileName.m"
:loop
tasklist /fi "imagename eq MATLAB.exe" |find ":" > nul
if errorlevel 1 goto loop
exit

Solution 5

I had the problem @John Gardeniers Had or described, where my exe called another exe and terminated, therefor start /wait didn't work. I created a "while loop" to check if its running and then move on once its done. The times can be tweaked to suit your needs.

TIMEOUT /T 60

SETLOCAL EnableExtensions
set EXE=MYEXETOCHECK.exe
:LOOPSTART
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
goto FIN
:FOUND
TIMEOUT /T 30
goto LOOPSTART
:FIN
Share:
393,842

Related videos on Youtube

Kravlin
Author by

Kravlin

I'm a student in college trying to learn IT and trying to be useful. not a lot more.

Updated on September 17, 2022

Comments

  • Kravlin
    Kravlin over 1 year

    I'm trying to create a script to execute an exe on shutdown in order to install sp1. my script goes something like (not actual bat script).

    If installed GOTO END
        Install.exe
    END:
    

    My problem is that when it runs, it starts the installer, then finishes the script because the installer's a different process and follows up by shutting down the install process because the computer's shutting down and shutting down the system (at least, that's what i think it's doing.)

    Is there any way to tell it to wait until the process it started completes and then shutdown?

  • mfinni
    mfinni about 13 years
    You could always add a loop to the shutdown routine to check to see if msiexec.exe (or whatever the hell it is) is running and then wait and loop again.
  • John Gardeniers
    John Gardeniers about 13 years
    @mfinni, just to complicate matters even further, msiexec often fires off other processes as well. :(
  • Alex Gordon
    Alex Gordon over 11 years
    great job with this answer!
  • Martin Prikryl
    Martin Prikryl over 10 years
    Actually, why do you need start /wait at all? I myself believed it's needed to use start /wait to block batch file until GUI (as opposite to a console) application finishes. But testing it now, I see that batch files actually wait even for GUI applications. I have posted corresponding question: Why GUI application blocks a batch file?
  • Erik
    Erik almost 10 years
    Assuming Matlab seems a bit strange to me, especially here on serverfault.
  • Oliver Bock
    Oliver Bock almost 10 years
    This can work for things other than MATLAB. Just change "MATLAB.exe" to something else. And perhaps fix that second 'find' to search for the exe again in the filtered output.
  • graywolf
    graywolf over 7 years
    you can always do cmd /C start /wait install
  • FreeSoftwareServers
    FreeSoftwareServers about 6 years
    I had this problem, added my solution as an answer.
  • Kyle Delaney
    Kyle Delaney almost 6 years
    What if you need to wait for a process that the batch file didn't start?
  • Jaime Penagos
    Jaime Penagos about 5 years
    @m4l490n - it will work ok. The redirection and conditional execution operators have the higher priority in the batch scripts (<,>,>>,&,&&,||,|)
  • Gulzar
    Gulzar almost 4 years
    doesn't work for me... Trying to invoke MSBuild (which takes like 2 min), and this returns immediately. How to debug this?
  • Babak
    Babak over 3 years
    Also, Try CALL Install.exe