HOWTO · Batch
Wait for a Command or Program to Finish in a Windows Batch File
Run commands in order in a Windows batch file: call another batch file with CALL, wait for a STARTed program with START "" /WAIT, and use TIMEOUT only for a delay.
Commands and executables that you run directly in a batch file already finish before cmd.exe reads the next line. Choose a special command only when you are calling another batch file or deliberately launching a program with START.
| Situation | Use this |
|---|---|
| An ordinary command or executable | Run it directly; the next line runs after it returns. |
Another .bat or .cmd file |
call child.cmd so control returns to the parent after the child ends. |
A program launched with START |
start "" /wait program.exe to wait for the process started by START. |
| A fixed delay or user confirmation | timeout or pause; neither detects process completion. |
This article shows each case and preserves the command’s exit code before another command can replace it.
Run an Ordinary Command or Executable Directly
When the command is on its own line, do not add START just to make the script wait. Save %ERRORLEVEL% immediately if the following steps need to know whether it succeeded.
@echo off
echo [1] ping start
ping 127.0.0.1 -n 2 >nul
set "rc=%ERRORLEVEL%"
echo [2] ping end: exit code=%rc%
exit /b %rc%
The output order is:
[1] ping start
[2] ping end: exit code=0
Here, ping returns before the second echo runs. The same ordering applies to an executable invoked directly. exit /b %rc% passes the saved result to a calling batch file or command shell.
Call Another Batch File and Return to the Parent
Calling a batch file by name transfers control to it. Use CALL when the parent must continue afterward.
parent.cmd:
@echo off
echo [parent] before call
call child.cmd
set "rc=%ERRORLEVEL%"
echo [parent] after call: exit code=%rc%
exit /b %rc%
child.cmd:
@echo off
echo [child] start
ping 127.0.0.1 -n 2 >nul
echo [child] end
exit /b 7
The parent receives control only after the child finishes:
[parent] before call
[child] start
[child] end
[parent] after call: exit code=7
The CALL command is the important part: without it, invoking child.cmd transfers execution to the child and the later parent lines do not run. Microsoft documents the command forms and return behavior in its call reference.
Pass arguments after the child filename, for example call child.cmd input.txt. Quote an argument that contains spaces, but keep CALL itself as the first command; using START for a child batch changes the launch model and is not a substitute for returning to the parent.
Wait for a Program Launched With START
START is useful when you intentionally launch a separate program. Add /WAIT to wait for the process that START launches:
start "" /b /wait "C:\Tools\My Program\worker.exe" --task import
set "rc=%ERRORLEVEL%"
echo worker exit code: %rc%
exit /b %rc%
The empty "" is the window title. It is required before a quoted executable path because START treats its first quoted argument as the title. /B keeps a console program in the current Command Prompt window; it is optional and does not make the command wait. /WAIT is the option that does that.
This form was checked with an executable copied to a temporary path containing spaces. It returned exit code 9, and the line after START ran only after that executable exited. See Microsoft’s start reference for the complete option list and How to Use the START Command in a Batch File for other launch patterns.
START /WAIT waits for the process it starts. It cannot guarantee that a launcher which starts a separate, detached child process has finished all of its own work. Check the launched program’s documentation when that distinction matters.
Use TIMEOUT or PAUSE Only to Pause the Script
Use TIMEOUT when you deliberately need an elapsed delay, not when you need proof that another process completed. Microsoft documents /NOBREAK for a countdown that ordinary key presses cannot skip, and TIMEOUT /T -1 as a key-press wait; PAUSE is another interactive prompt. The Windows TIMEOUT command accepts values from -1 through 99999 seconds. A delay may be too short or unnecessarily long, so use CALL or START "" /WAIT when process completion is the condition you need.
For options and examples dedicated to timed delays, see How to Use TIMEOUT in a Batch File.
Troubleshooting
- Do not use a process-name polling loop as a general replacement for
START /WAIT. Multiple processes can share a name, and a launcher can return before a detached child finishes. - Read
%ERRORLEVEL%immediately after the command you care about.echo,set, and later commands can change it. - Do not add
cmd /kto automation examples: it deliberately leaves a Command Prompt open instead of reporting whether the script completed. - If a program needs administrator rights, user input, network access, or an application-specific completion signal, its own documentation determines whether command-process waiting is sufficient.
Summary
Run ordinary commands directly. Use CALL for another batch file that must return to its caller, and use START "" /WAIT only when you launch a program through START. TIMEOUT and PAUSE pause the batch file but do not tell you whether a program has completed.