Windows OS

Batch File Pause and Wait Commands: How to Control the Flow of Your Scripts

In batch scripting on Windows, the developers can control the flow of your scripts using various techniques or methods to introduce pauses or waits. The “pause” and “wait” commands are two ways to control the flow of your batch files. The “pause” command stops the execution of a batch file while presenting the “Press any key to continue…” message. The batch file cannot run until the user presses a key. The batch file’s execution is suspended for the number of seconds indicated by the wait instruction.

Batch File Pause Command

A batch file’s “pause” command stops the batch file execution while displaying the message on the command line. The batch file won’t run until the user presses a key. The command that stops the next execution is helpful. Running the batch file directly and seeing the desired output appear on the screen is beneficial.

The syntax of the “pause” command is:

Pause

Prompt the User for Input

The “pause” command in the following batch script shows the “Press any key to continue…” message when the user runs this batch file. Before the batch file moves on to the “dir” command, the user must press a key.

@echo off

rem This batch file pauses for the user to press a key.

echo Hello, Welcome to PAUSE command demonstration.

echo Script execution continues after the pause.

pause

rem This batch file continues with the next command.

echo Thank you!

dir

When the user presses any key using the keyboard, this batch process continues:

Batch File Wait Command

The code that you provided is a batch file that opens two programs: Notepad and Paint. The code is as follows:

@echo off

echo Starting First Program.

START /B /WAIT notepad.exe

echo Notepad Opened in Background successfully

echo Starting Second Program.

START /WAIT mspaint.exe

echo The Paint is Opened successfully.

cmd /k

The first line, “@echo off”, prevents the batch file from displaying the commands as they are executed.

The user is simply informed that the first program is being launched by the second line which is “echo Starting First Program”.

The third line, “START /B /WAIT notepad.exe”, opens the Notepad program in a background window and waits for it to finish before continuing. The “/B” switch tells the START command to run the program in a background window, and the “/WAIT” switch tells the START command to wait for the program to finish before continuing.

The fourth line reads the echo Notepad. When Notepad is successfully opened in the background, it merely notifies the user with a message.

The fifth line, “echo Starting Second Program”, simply displays a message to the user that the second program is being started.

The sixth line, “START /WAIT mspaint.exe”, opens the Paint program in a background window and does not wait for it to finish before continuing.

The seventh line, “echo The Paint is Opened successfully”, simply shows a message to the user that the Paint has been opened successfully.

The eighth line, “cmd /k”, opens a new command prompt window and keeps the current command prompt window open.

Using a Loop for Custom Delays

We can use a loop for custom delays in the batch process. Here is an example:

In this example, the first message is shown to the user when this batch file is run. The first message is then displayed once more once the “for” loop begins. The timeout command stops the batch file for one second before displaying the second message. The final message is then shown after a total of 10 iterations.

@echo off

echo Demonstration of Delay using Loop

for /l  %%i in (1,1,10) do (

timeout /t 1 /nobreak > nul

echo This message display after %%i  second(s)

)

echo This message displayed a 10-seconds delay.

Using Timeout for Delay

A batch file’s timeout command pauses batch file execution for a predetermined number of seconds. The syntax is given here:

timeout /t <SECONDS_DELAY> [/nobreak]

The “/t” switch specifies the amount of time in seconds to wait. The “/nobreak” switch disables the key presses from the user to break the delay.

The “timeout” command can be used to:

  • Postpone a batch file’s execution for a predetermined period of time
  • Prevent a batch file action from being interrupted by the user
  • Create a more user-friendly experience by preventing the user from having to press a key to continue

For example, the following batch file pauses for 10 seconds:

@echo off

echo Hello, Script execution continues after 10 seconds of delay

timeout /t 10

echo Thank you!

As demonstrated in the following screenshot, a timer is running and waiting for the user to interrupt the “timeout” command by pressing any key. If the user presses any key, the process stops and moves to the next command in the batch file:

The user in the following screenshot interrupts the “timeout” command after 5 seconds, resulting in the display of the “Thank you!” message:

The following screenshot shows how the “Thank You!” message takes 10 seconds to print on the screen if the end-user doesn’t interrupt the timeout instruction:

Here is the updated code that has been modified to include the no-break switch that stops the end-user from interrupting a batch file process:

@echo off

rem This batch file pauses for 10 seconds

echo Hello, Script execution continues after 10 seconds of delay

timeout /t 10 /nobreak

rem After 10 seconds this command will be displayed on the prompt screen

echo Thank you!

Here is the output of the previous code:

The command line prompts the user to type either Y or N if the user uses the “CTRL+C” command. If the user clicks Y, this batch job is stopped:

The “pause”, “wait”, and “timeout” commands are the ways to control the flow of our scripts. The script is suspended from running when the “pause” command is used, and it is stopped for a predetermined amount of time when the “timeout” command is used.

Using Conditional Statements for Flow Control

Conditional statements are the most commonly feasible technique that is employed to modify a script’s flow based on a certain circumstance. Based on whether a condition is true or false, the “if” statement performs one of two pieces of code execution. The “if” statement is structured as follows:

if <condition> <true_BLOCK> else <false_BLOCK>

Example:

Open the Notepad or any text editor of your choice and write the following code which initializes the value of the inputValue variable to 1 in the third line. The value of the inputValue variable is then checked using an “if” statement on run time to see if it equals 1, 2, or 3. If so, the appropriate message is printed on the command line screen. If it is not, the final message is displayed. In a conditional statement known as a “if”, a variable’s inputValue is checked to see if it equals 1, 2, or 3. If it does, one of two code blocks is then executed. If the condition is true, the conditional statement does nothing.

@echo off

rem This script tests the variable inputValue against the values 1, 2, and 3.

set inputValue=1

if "%inputValue%"=="1" (

    echo The input value is equal to 1.

) else if "%inputValue%"=="2" (

    echo The input value is equal to 2.

) else if "%inputValue%"=="3" (

    echo The input value is equal to 3.

) else (

    echo The variable inputValue is not equal to 1, 2, or 3.

)

Output:

Conclusion

Script flow may be controlled, and processes can be automated on the Windows operating system using commands, loops, and conditional statements like “if”. Developers may stop a script’s execution or add delays with the “pause” command by utilizing “timeout” or “ping.” Although there is no built-in “switch” statement in batch scripting, we can simulate it with “if” and “else if” expressions. Testing is crucial to ensure that these scripts function as intended in the appropriate context. These scripts are useful for automation, system management, and file manipulation.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content