How to Delay or Wait for a Command to Run a Process in a Windows Batch File

A batch file is a script that automates processes on the Windows operating system. In addition, a batch file is a simple text file that contains a series of commands executed sequentially, one line at a time.

Batch files are commonly used to perform repetitive or tedious tasks, such as copying files, launching programs, or running other scripts.

file batch wait

To create a batch file in Windows, you can use any text editor, such as Notepad. After you write the command you want to run, you need to save the file with the file extension .bat. You can then run the batch file by double-clicking on it.

When the Batch file is run, it does not wait for the command process to finish to run the next command, instead; it runs all commands line by line. It’s important to make those commands wait until the process completes before executing the next command.

There are various simple ways to delay or wait for a process to finish before running the next command in your batch file.

 “/WAIT” command to wait for the process to finish

When starting a program in a batch file using the “START” command, you can add  “/wait” to wait for the process to complete. Although there are many commands,  “/wait” can wait for each command to finish before moving on to the next command.

The “/B” argument can run the same process without opening a new window. START without the /B argument will launch the application or command in a new window.

Example of waiting for a process to finish before running the next command

@echo off
echo starting first program.
START /B /WAIT cmd /c "D:\first_script.bat"
echo The first script is executed successfully.
START /B /WAIT cmd /c "D:\second_script.bat"
echo The second script is executed successfully.
cmd /k

An example of running the notepad application that will open the mspaint application when the notepad is closed.

@echo off
echo starting first program.
START /B /WAIT notepad.exe
echo The first program is executed successfully.
START /B /WAIT mspaint.exe
echo The second program is executed successfully.
cmd /k

cmd /c Run Command then end

cmd /k Run Command and then return to the CMD prompt.

“Timeout” command to delay in seconds

You can use the “timeout” command to pause for a few seconds or the user presses a button before proceeding to the next command.

For example, to wait for 30 seconds before running the next command.

/t 30 timeout

An example is to wait for 30 seconds and prevent the user from stopping the pause with keystrokes.

timeout /t 30 /nobreak

An example is to wait for the user to press a button before executing the next command.

timeout /t -1

“Pause” command to wait for the user to press the button

To suspend the batch file until the user taps the button, use the “pause” command. This direct command requires no sign and can be used anywhere in your script to wait for the user to press the button.

When the “pause” command is executed, the user will see “Press any key to continue . . . on the screen.

You can use “pause” just before the command line that you don’t want to process before the user presses any key.

RELATED ARTICLES

Latest Articles