How to Wait for Command to Finish Execution in Windows Batch File
-
Use
/WAIT
to Wait for the Command to Finish Execution in Batch Script -
Use the
TIMEOUT
Command to Delay the Execution in Batch Script -
Use the
PAUSE
Command to Pause the Execution in Batch Script -
Use the
CALL
Command to Wait for the Command to Finish Execution in Batch Script -
Use the
&&
Operator to Wait for Command to Finish Execution in Batch Script - Use a Loop to Wait for Command to Finish Execution in Batch Script
- Conclusion
Batch scripting in Windows allows users to automate tasks by writing a series of commands that the system can execute. Often, it’s crucial to ensure that a command finishes its execution before moving on to the next one.
There are multiple commands and installation processes in a Batch file that usually take some time to complete. But when a Batch file is run, it does not wait for a command process to finish; it executes all commands line by line.
It is important to make those commands wait for them to finish and then execute the next commands. For a process to wait until it is finished, we use the /wait
parameter with the START
command.
Instead of starting a command, if there is a need to insert delays in the Batch file for some time interval, we can use commands such as TIMEOUT
and PAUSE
to stop the execution of the next process for a short interval of time or until a key is pressed.
This tutorial illustrates different ways to wait for a command or a program to finish before executing the next command in the Windows Batch file.
Use /WAIT
to Wait for the Command to Finish Execution in Batch Script
The /WAIT
parameter is used with the START
command in a Batch file. It instructs the command prompt to wait for the command or application to complete its execution before moving on to the next line in the Batch script.
This parameter is particularly useful when you need to launch an application or script and ensure it finishes before proceeding.
The syntax of the START
command with the /WAIT
parameter is as follows:
START "title" [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/AFFINITY <hex affinity>] [/WAIT] [/B] [command/program]
[parameters]
/WAIT
: This is the parameter we’re focusing on in this guide. It causes theSTART
command to wait for the executed command or program to finish before proceeding with the Batch script.
When we start a program in a Batch file using the START
command, we can wait until the program is finished by adding /wait
to the START
command. Even if there are multiple commands, /wait
can be used for each process to finish and move to the next one.
Also, the parameter /B
is used to stay in the same process without creating a new window. The START
command without the /B
parameter opens the program or command in a new window.
Wait for a Command to Finish Execution
For example, we need to wait for a command to finish execution before running the next one.
@echo off
echo starting first program.
START /B /WAIT cmd /c "C:\Users\Aastha Gas Harda\Desktop\testfile1.bat" > output.txt
echo The first program is executed successfully.
START /B systeminfo >> stdout.txt
echo All the programs are executed successfully
cmd /k
This Batch script executes two programs sequentially. It starts by running testfile1.bat
and waits for it to finish before echoing a success message.
Then, it gathers system information using systeminfo
and appends the output to output.txt
. Finally, it opens a new command prompt window for further interaction.
Output:
Wait for the .exe
File to Finish Execution
Another example is where we need to run a .exe
file and wait until the execution is done completely.
@echo off
echo starting first program.
START /B /WAIT JRuler.exe
echo The first program is executed successfully.
START /B systeminfo >> output.txt
echo All the programs are executed successfully
cmd /k
This Batch script first runs an application called JRuler.exe
and waits for it to finish. It then echoes a success message.
Next, it gathers system information using the systeminfo
command and appends the output to a file called output.txt
. Finally, it opens a new command prompt window for further interaction.
Output:
As soon as you close the .exe
file, the second program will begin execution. cmd /k
in the last line is used to prevent the command prompt from exiting after execution.
If there are multiple programs, you can use /WAIT
with each command to wait until the execution is finished. The START
command with the /WAIT
parameter doesn’t have any timeout, i.e., it does not matter how long the process will take to finish; it will wait until the process is completed.
@echo off
START /WAIT install1.exe
START /WAIT install2.exe
/WAIT
can only be used with the START
command. We can insert a time delay for other commands by using the TIMEOUT
and PAUSE
commands.
Use the TIMEOUT
Command to Delay the Execution in Batch Script
The TIMEOUT
command is used in Batch scripts to introduce a delay in the execution. It allows you to pause the script for a specified amount of time, measured in seconds.
This is particularly useful when you need to ensure that a process or task is completed before moving on to the next step.
The range for the TIMEOUT
command varies between -1
and 100000
. If the delay is set to -1
, it will act as a pause
command to wait until a key is pressed.
As in the previous method, we can replace the /wait
by inserting the TIMEOUT
command with the /t
parameter. The syntax for the TIMEOUT
command is given below:
TIMEOUT [/T timeout] [/NOBREAK]
/T timeout
: This option specifies the delay time in seconds. If omitted, the default delay is10
seconds./NOBREAK
: This option allows users to interrupt the countdown by pressing any key. If omitted, the countdown cannot be interrupted.
Let’s take the example in the previous method and add a time delay of 30
seconds after the execution of the first program.
@echo off
echo starting first program.
START /B JRuler.exe
TIMEOUT /t 30
echo The first program is executed successfully.
START /B systeminfo >> output.txt
echo All the programs are executed successfully
cmd /k
This Batch script automates the execution of two programs. It starts with JRuler.exe
and waits for 30
seconds.
Afterward, it echoes a success message. Then, it runs the systeminfo
command and appends the output to output.txt
.
Finally, it opens a new command prompt window for further interaction.
Output:
After 30
seconds, the second program will begin execution. Also, if a user presses a key before the timeout, the second program will begin execution.
To prevent user keystrokes, use the /nobreak
parameter with the TIMEOUT
command. This will ignore any key presses by the user.
However, you can stop the delay by pressing the Ctrl+C, which will raise the errorlevel1
.
Use the PAUSE
Command to Pause the Execution in Batch Script
The PAUSE
command in Batch scripting serves as a mechanism to temporarily halt the execution of a script. It displays the message "Press any key to continue..."
and waits for user input.
This is especially useful when you want to give users an opportunity to review information or confirm an action before proceeding with the script.
The PAUSE
command is simple and does not require any additional parameters or options. It is used as follows:
PAUSE
When the PAUSE
command is encountered in a Batch script, it prompts the user to press any key. Once a key is pressed, the script continues its execution.
The PAUSE
command is used to pause the execution of a Batch file until a key is pressed. It is useful if the user wants to read the output text or wait until a process is finished.
However, there is no timeout, and it will only continue until the user presses a key.
@echo off
echo starting first program.
START /B cmd /c "C:\Users\Aastha Gas Harda\Desktop\testfile1.bat" > output.txt
echo The first program is executed successfully.
PAUSE
START /B systeminfo >> output.txt
echo All the programs are executed successfully
cmd /k
This Batch script automates the execution of two programs. It starts with testfile1.bat
, captures its output to output.txt
, and confirms successful execution.
The script then prompts the user to press a key to continue. Next, it runs the systeminfo
command and appends its output to output.txt
.
Finally, it opens a new command prompt window for further interaction.
Output:
All the methods mentioned above work fine. If you use the START
command, it is recommended to use /wait
instead of delay commands as the process may take longer than specified.
Use the CALL
Command to Wait for the Command to Finish Execution in Batch Script
The CALL
command in Batch scripting is used to run another Batch file within the current script.
It essentially creates a temporary subroutine, allowing the original script to wait for the called script’s completion before proceeding. This makes it an effective method for synchronizing the execution of commands.
The syntax of the CALL
command is as follows:
CALL :label arguments
CALL [drive:][path]filename [arguments]
:label arguments
: This form is used to call a subroutine within the same Batch file, identified by a label.[drive:][path]filename [arguments]
: This form is used to call an external Batch file by specifying its full path along with any required arguments.
Let’s explore practical examples to demonstrate the use of the CALL
command.
Calling a Subroutine within the Same Batch File
@echo off
echo Starting Process I
CALL :ProcessI
echo Process I completed.
pause
exit
:ProcessI
echo Performing Process I...
:: Add your commands for Process I here
:: For example, "ping -n 5 127.0.0.1" simulates a delay
ping -n 5 127.0.0.1
exit /b
- In this script, we have a main section and a subroutine (
:ProcessI
). - The
CALL :ProcessI
command invokes the subroutine. The execution of the main Batch file will pause until:ProcessI
completes. - The
exit /b
command at the end of:ProcessI
is used to return to the main Batch file after completion.
Output:
Calling an External Batch File
This script uses CALL
to execute an external Batch file named testfile2.bat
located on the desktop.
@echo off
echo Starting Process J
CALL "C:\Users\Username\Desktop\testfile2.bat"
echo Process J completed.
pause
exit
This script automates the execution of an external Batch file, captures its output, and provides feedback along the way. The user is prompted to press a key to continue after the program finishes.
Output:
Use the &&
Operator to Wait for Command to Finish Execution in Batch Script
The &&
operator in Batch scripting allows you to execute commands sequentially, where the second command only runs if the first one succeeds. This creates a dependency between the commands, ensuring that they execute in a specific order.
The syntax of the &&
operator is as follows:
command1 && command2
command1
: This is the first command that is executed.command2
: This is the second command that is only executed ifcommand1
succeeds (returns a zero exit code).
Let’s explore practical examples to demonstrate the use of the &&
operator.
Example 1: Running Commands Sequentially
@echo off
echo Starting Process K
echo Performing Process K... && ping -n 5 127.0.0.1
echo Process K completed successfully.
pause
exit
- In this script, the
echo Performing Process K...
command is followed by&&
, which means the next command (ping
) will only execute if the first command is successful. - The
ping
command simulates a delay by waiting for5
seconds (-n 5
) on the local machine (127.0.0.1
). - The script then echoes a message indicating successful completion.
Output:
Example 2: Using Conditional Execution with Batch Files
@echo off
echo Starting Process L
call :ProcessL && (
echo Process L completed successfully.
) || (
echo Process L encountered an error.
)
pause
exit
:ProcessL
echo Performing Process L...
:: Add your commands for Process L here
:: For example, "dir" lists files in the current directory
dir
exit /b
- In this script, the
call :ProcessL
command is followed by&&
. - The subsequent block of code within the
(
and)
is executed only if thecall :ProcessL
succeeds (returns a zero exit code). - If the
call :ProcessL
returns a non-zero exit code, the code after||
is executed instead. - This demonstrates how to conditionally execute different blocks of code based on the success of a preceding command.
Output:
Use a Loop to Wait for Command to Finish Execution in Batch Script
In Batch scripting, a loop is a control structure that allows a set of commands to be executed repeatedly. By utilizing a loop, you can continuously check the status of a process until it completes.
This approach is particularly useful for scenarios where you need to wait for a specific task to finish before proceeding.
Let’s delve into a practical example to demonstrate how to use a loop to wait for a command to complete.
@echo off
echo Starting Process K
echo Performing Process K...
:CheckProcess
tasklist | find /i "process_name.exe" >nul
if %errorlevel% neq 0 (
timeout /t 2 >nul
goto :CheckProcess
)
echo Process K completed successfully.
pause
exit
The Batch script begins with turning off command echoing using @echo off
. It echoes a message indicating the start of a process (Process K
).
Then, the :CheckProcess
label marks the start of a loop. This is where the script checks the status of the target process.
tasklist | find /i "process_name.exe" >nul
retrieves a list of running processes and searches for a process with the specified name (replace process_name.exe
with the actual process name). The >nul
part suppresses the output.
Next, the if %errorlevel% neq 0
checks the return code of the previous command. If it’s not equal to 0
(indicating that the process is still running), the loop continues.
timeout /t 2 >nul
introduces a 2
second delay using the timeout
command. The goto :CheckProcess
redirects the script back to the :CheckProcess
label, effectively creating a loop.
Once the process is no longer found, the script proceeds. Finally, it echoes a message confirming the successful completion of Process K
and waits for user confirmation before exiting.
Output:
Conclusion
This tutorial explores various methods to wait for a command to finish execution in Windows Batch scripting.
- Using
/WAIT
withSTART
: This method ensures the script waits for a command or application to complete using the/WAIT
parameter with theSTART
command. It’s ideal for launching applications that need to finish their tasks. - Introducing Delays with
TIMEOUT
: TheTIMEOUT
command allows for a specified delay in script execution. This is helpful when you want to provide a process with enough time to complete. - Temporary Halt with
PAUSE
: ThePAUSE
command temporarily stops script execution and prompts the user to press a key before continuing. This is useful for user interaction and confirmation. - Using
CALL
for Script Synchronization: TheCALL
command enables the execution of another Batch file within the current script, creating a temporary subroutine. This allows the original script to wait for the called script’s completion. - Conditional Execution with
&&
: The&&
operator ensures that the second command only runs if the first one succeeds, establishing a dependency between commands. This is useful for sequential execution. - Implementing Loops for Continuous Checking: A loop is a control structure that repeatedly checks the status of a process until it completes. This is valuable for scenarios where you need to wait for a specific task to finish.
By understanding these methods, you have gained a comprehensive toolkit for handling command execution and synchronization in Windows Batch scripting. These techniques are invaluable for automating various tasks efficiently.