7

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

Stephen
  • 1,607
  • 2
  • 18
  • 40
  • 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 Answers6

15
START /WAIT First.exe
START /WAIT Second.exe
aphoria
  • 19,796
  • 7
  • 64
  • 73
7

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
  • 2,988
  • 3
  • 32
  • 47
6

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.

Toon Krijthe
  • 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
4

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

3

Here is the very simple process. Just keep the following line to wait for 10 secs and then continue the other commands...

TIMEOUT /T 10

That's all you have to do. I hope you enjoy my technique.

GDP
  • 8,109
  • 6
  • 45
  • 82
Raghu
  • 31
  • 1
0

You can use

PAUSE

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

Dean Rather
  • 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