HOME
Stephen Knight
10/20/2009
08:51 AM
Simple pause / delay for batch file
Type:
Batch/Command file
Category:
Delay, Pause, Wait, Sleep
There are many ways of causing a pause in a batch file but the easiest assuming that at least one network interface is connected is to use a ping to the localhost because by default a PING is done every 1 second. The first one is nearly immediate so n+1 pings are needed to make n seconds delay.
You can also use the built in function "TIMEOUT" - e.g.
TIMEOUT /T 5
That shows a countdown of how long is left and allows you to press a key to continue. Adding /NOBREAK means pressing a key does not continue - can be stopped using Control C or closing the window as normal.
Code
Explanation
:wait
set delay=%1
if [%delay%]==[] set delay=5
set /a delay+=1
ping 127.0.0.1 -n %delay% >NUL
exit /b
Line label to call subroutine using call :wait x where x is the number of seconds
Assign the value of "x" to the delay variable
Check if no value has been supplied and if so delay for 5 seconds
Add one to the delay value as 6 pings is approx. 5 seconds
PING localhost x+1 times, hiding the output
Return to the next line after the CALL in the batch file
Examples
Attachments