tags:

views:

1980

answers:

5

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)?

A: 

You can use

PAUSE

In batch scripting, but I don't understand your question.

Dean
+5  A: 

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.

Gamecat
Try that with explorer.exe and you'll see it returns after the program has launched, not finished.
paxdiablo
@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
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
+5  A: 

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.

kdmurray
The proper way is actually: PING 127.1 -n 10 -w 1000 >NUL
djangofan
+3  A: 

You could use the "start" command with parameter /wait use start /? on commandline for details.

Try as I might, I couldn't get "start /wait explorer.exe" to actually wait...
paxdiablo
+6  A: 
START /WAIT First.exe
START /WAIT Second.exe
aphoria