I am writing a batch file, in which I call an EXE to execute. Now, statements after the call to the EXE should not execute till the EXE completes its execution. How can I do it in the batch file (on Windows)?
-
I think this depends a bit on the applications you're running. If the application is a launcher that launches other services or a GUI, then I think there's little you can do without writing an application to monitor processes and/or windows. – Hand-E-Food Aug 23 '11 at 02:00
6 Answers
It depends on how the .exe works. I'm afraid I don't have all the technical details or terminology, but some .exe files will return control of the session immediately after they've started, while others won't return control until after the program has terminated.
The second case is easy as the commands late in the file won't execute until the former have completed, so I'll assume you're facing case #1.
An easy workaround/hack if the execution takes (approximately) the same amount of time every time it runs is to use a ping command with a delay.
PING 127.0.0.1 -n 1 -w 120000 >NUL
This will force the ping command to run once with a 120000ms (2 min) delay.
There's also a good article on a more complex (but more reliable method) on fpschultze.de with a much more detailed explanation. In short you query the task list searching for the executable you're waiting for. As soon as it's not there you carry on with the batch file. It also uses the ping method, but in a different manner.

- 2,988
- 3
- 32
- 47
-
1
-
Sorry, but the proposed ping workaround is just terrible. It is prone to errors and difficult to understand. There are far superior ways to pause for some time (e.g. timeout). – mafu May 16 '11 at 09:22
-
-
`ping 127.0.0.1 -n 13 > nul` to wait 12 seconds, from https://stackoverflow.com/a/1672349/984471 – Manohar Reddy Poreddy Dec 28 '18 at 04:15
The statements in a batch file are executed sequentially. So if your batch file looks like:
first.exe
next.exe
Next is executed if first is completed.

- 52,876
- 38
- 145
- 202
-
Try that with explorer.exe and you'll see it returns after the program has launched, not finished. – paxdiablo Dec 08 '08 at 09:33
-
@Pax: it depends on the scope of the question. In general, in a batch file, you generally put only command line tools, not GUI tools. – PhiLho Dec 08 '08 at 09:49
-
Usually but not always - I have a startup batch file in my work-from-home virtual box which starts the graphical VPN client (then mail, bug tool, etc). It luckily has a command-line switch which cause it to not return until finished as I couldn't get start /wait to do it. But your point is taken. – paxdiablo Dec 08 '08 at 10:22
You could use the "start" command with parameter /wait use start /? on commandline for details.
You can use
PAUSE
In batch scripting, but I don't understand your question.

- 31,756
- 15
- 66
- 72
-
`Pause` wais for the user to press a key. That won't work for an automated script as the user isn't there. James' wants an automated script to run one program, wait for it to finish and exit, then run a second program. – Hand-E-Food Aug 23 '11 at 01:57