HOME
Stephen Knight
10/20/2009
09: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
@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
Explanation
Examples
Attachments