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

bardimin pic

Bardimin

February 13, 2023
file batch wait

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

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

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

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 . 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 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 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 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 that you don't want to process before the user presses any key.

link nyemin

Latest Articles

How to Sync Files between Different Computers Easily and Quickly

How to Sync Files between Different Computers Easily and Quickly

Have you ever had trouble syncing files between different computers? Do you want to have the same file on all your computers without having to copy and paste it manually? Do you want to know how to synchronize files between different computers easily and quickly? If...

How to Display Caps Lock Indicator on Windows 11 Screen

How to Display Caps Lock Indicator on Windows 11 Screen

Do you often type in uppercase letters accidentally because you don't realize that the Caps Lock key is active? Do you have trouble seeing the Caps Lock indicator on your keyboard because it's too small or not there at all? Do you want to get visual or audible...

How to Find Out a New or Used Hard Drive Easily and Accurately

How to Find Out a New or Used Hard Drive Easily and Accurately

A hard drive is one of the important components of a computer because it functions as a data storage medium. However, not all hard drives sold in the market are new hard drives. There are also used hard drives that are sold at a cheaper price but have a higher risk of...

How to Enter Full-Screen Mode in Windows 11 Easily and Quickly

How to Enter Full-Screen Mode in Windows 11 Easily and Quickly

One feature you can enjoy in Windows 11 is the full-screen mode, which lets you maximize your screen space by hiding unnecessary elements, such as the taskbar, menus, and address bar. Full-screen mode can enhance your experience when using apps, watching videos,...

What impact does the CPU run 100% for a long time?

What impact does the CPU run 100% for a long time?

If you've ever experienced your CPU operating at 100% for any length of time, you may wonder if this can damage your computer's hardware. The CPU (Central Processing Unit) is the main component of the computer that carries out program instructions. The CPU has a...