timeout - Wait
There may be times when you need to implement a process that waits for a certain amount of time while using a batch file.
For example, you may need to write a process that waits for the completion of a file copy in a batch file.
This page provides a clear explanation of how to use the timeout
command to wait for a specified time in the command prompt or batch file, including how to set options.
Basic Usage of the timeout
Command
To implement a process that waits for a certain amount of time in the command prompt, use the timeout
command.
Use it when you want to set a certain waiting time between a series of commands executed in order in a batch file.
The usage of the timeout
command is as follows:
timeout /t <seconds> [/nobreak]
The timeout
command requires the specification of options, and if executed without options, it will result in an error as shown below.
When using the timeout
command, it operates by specifying the /t
option followed by the number of seconds to wait.
For example, to wait for 100 seconds, use the following command:
You may have noticed that the timeout
command does not always wait for the specified time. The correct behavior is that it waits for the specified time only if the user does not perform any action.
This means that even if you specify timeout /t 100
, the user can interrupt the waiting time by pressing any key.
To disable this behavior, use the /nobreak
option.
By specifying the above, the waiting time cannot be interrupted even if the user presses a key.
Pressing Ctrl + C will interrupt the waiting time and also terminate the execution of the batch file.
Examples of the timeout
Command
Waiting Indefinitely Until the User Takes Action
By specifying -1
for the /t
option, you can wait indefinitely until the user takes some action.
Adding a Wait Time Between Commands in a Batch File
To add a wait time between commands in a batch file, use the following syntax:
@echo off
setlocal
echo Process 1
timeout /t 100
echo Process 2
endlocal
In the above case, since the /nobreak
option is not specified, the waiting time can be interrupted by user action.
Adding a Wait Time Between Commands in a Batch File
To prevent the waiting time from being interrupted, specify as follows:
@echo off
setlocal
echo Process 1
timeout /t 100 /nobreak
echo Process 2
endlocal