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.

Hide details for CodeCode
@echo off
cls
echo The time is %time%, Pausing for 10 seconds
call :wait 10

echo The time is %time%, Pausing for 5 seconds
call :wait

echo The time is %time%, Press any key to end
pause >nul
exit /b

:wait
REM call this subroutine with the number of seconds to pause. Default with none is 5.
set delay=%1
if [%delay%]==[] set delay=5
set /a delay+=1
ping 127.0.0.1 -n %delay% >NUL
exit /b


Hide details for ExplanationExplanation

: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


Hide details for ExamplesExamples


Hide details for AttachmentsAttachments
waittest.cmd