To make a batch file wait for a number of seconds there are several options available:
|Note:||Click a script file name to expand and view its source code; click the file name again, or the expanded source code, to hide the source code again.
To view the source code on its own, right-click the file name and choose Open or Open in separate tab or window.
The most obvious way to pause a batch file is of course the
This will stop execution of the batch file until someone presses "any key".
Well, almost any key: Ctrl, Shift, NumLock etc. won't work.
This is fine for interactive use, but sometimes we just want to delay the batch file for a fixed number of seconds, without user interaction.
SLEEP was included in some of the Windows Resource Kits.
It waits for the specified number of seconds and then exits.
will delay execution of the next command by 10 seconds.
There are lots of
SLEEP clones available, including the ones mentioned in the UNIX Ports paragraph at the end of this page.
TIMEOUT was included in some of the Windows Resource Kits, but is now a standard command in Windows 7 and 8 (not sure about Vista).
It waits for the specified number of seconds or a keypress, and then exits.
TIMEOUT's delay can be "bypassed" by pressing a key.
TIMEOUT /T 10
will delay execution of the next command by 10 seconds, or until a key is pressed, whichever is shorter.
D:\>TIMEOUT /T 10 Waiting for 10 seconds, press a key to continue ...
You may not always want to abort the delay with a simple key press, in which case you can use
D:\>TIMEOUT /T 10 /NOBREAK Waiting for 10 seconds, press CTRL+C to quit ...
You can still abort the delay, but this requires Ctrl+C instead of just any key, and will raise an ErrorLevel 1.
For any MS-DOS or Windows version with a TCP/IP client,
PING can be used to delay execution for a number of seconds.
If specified (-w switch),
PING will wait for a number of milliseconds between two pings before giving a time-out.
PING 188.8.131.52 -n 1 -w 60000 >NUL
will delay execution of the next command 60 seconds, provided 184.108.40.206 is not a valid IP address (I previously used
-n 60 -w 1000 which should theoretically result in the same delay, but as Greg Hassler pointed out this may be highly inaccurate on some computers).
My previous information that you should PING to localhost using 1 extra ping was incorrect for intervals other than 1 second, as Todd Renzema pointed out to me.
He found out that if the ping does not time out, as is the case when pinging to localhost, the next ping will be about 1 second later, no matter what time-out interval is specified.
To use the time-out specified, ping to a non-existent IP address, and do not add an extra ping. Do not ping to a non-existent host name, since that will only result in a "Unknown host" error message within a second.
And, as Les Ferch pointed out, your computer should be connected to the network for the
PING delay to work, otherwise you'll immediately get an error message, and no delay.
With the advance of TCP/IPv6 the command has to include that as well:
PING 127.0.0.1 -n 60 >NUL 2>&1 || PING ::1 -n 60 >NUL 2>&1
Summarizing: probably the safest, though not the most accurate
PING based delay uses 127.0.0.1 and ::1, and the standard 1 second ping interval instead of a specified time-out interval, i.e.
PING 127.0.0.1 -n 6 || PING ::1 -n 6 for a 5..6 seconds delay.
PING time-out technique is demonstrated in the following examples:
@ECHO OFF :: Check Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Check if a valid timeout period is specified IF "%~1"=="" GOTO Syntax IF NOT "%~2"=="" GOTO Syntax ECHO.%*| FINDSTR /R /X /C:"[0-9][0-9]*" >NUL || GOTO Syntax IF %~1 LSS 1 GOTO Syntax IF %~1 GTR 3600 GOTO Syntax :: Use local variable SETLOCAL :: Add 1 second for IPv4 SET /A seconds = %1 + 1 :: The actual command: try IPv4 first, if that fails try IPv6 PING -n %seconds% 127.0.0.1 >NUL 2>&1 || PING -n %1 ::1 >NUL 2>&1 :: Done ENDLOCAL GOTO:EOF :Syntax ECHO. ECHO PMSleep.bat ECHO Poor Man's SLEEP utility, Version 3.00 for Windows NT 4 and later. ECHO Wait for a specified number of seconds. ECHO. ECHO Usage: CALL PMSLEEP seconds ECHO. ECHO Where: seconds is the number of seconds to wait (1..3600) ECHO. ECHO Notes: The script uses PING for the delay, so an IP stack is required. ECHO The delay time will not be very accurate. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com IF "%OS%"=="Windows_NT" EXIT /B 1
@ECHO OFF :: Check if a timeout period is specified IF "%1"=="" GOTO Syntax :: Filter out slashes, they make the IF command crash ECHO.%1 | FIND "/" >NUL IF NOT ERRORLEVEL 1 GOTO Syntax :: Check for a non-existent IP address :: Note: this causes a small extra delay! IF "%NonExist%"=="" SET NonExist=10.255.255.254 PING %NonExist% -n 1 -w 100 | FIND "TTL=" >NUL IF ERRORLEVEL 1 GOTO Delay SET NonExist=220.127.116.11 PING %NonExist% -n 1 -w 100 | FIND "TTL=" >NUL IF NOT ERRORLEVEL 1 GOTO NoNonExist :Delay :: Use PING time-outs to create the delay PING %NonExist% -n 1 -w %1000 >NUL :: Show online help on errors IF ERRORLEVEL 1 GOTO Syntax :: Done GOTO End :NoNonExist ECHO. ECHO This batch file needs an invalid IP address to function ECHO correctly. ECHO Please specify an invalid IP address in an environment ECHO variable named NonExist and run this batch file again. :Syntax ECHO. ECHO PMSlpW9x.bat ECHO Poor Man's SLEEP utility, Version 2.10 for Windows 95 / 98 ECHO Wait for a specified number of seconds. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO Corrected and improved by Todd Renzema and Greg Hassler ECHO. ECHO Usage: CALL PMSLPW9X nn ECHO. ECHO Where: nn is the number of seconds to wait ECHO. ECHO Example: CALL PMSLPW9X 10 ECHO will wait for 10 seconds ECHO. ECHO Note: Due to "overhead" the actual delay may ECHO prove to be up to a second longer :End
Download the PMSleep sources
NETSH may seem an unlikely choice to generate delays, but it is actually much like using
NETSH Diag Ping Loopback
will ping 127.0.0.1, which takes about 5 seconds — hence a 5 seconds delay.
NETSH is native in Windows XP Professional and later versions.
Unfortunately however, this trick will only work in Windows XP/Server 2003.
REM |before the CHOICE command, the standard input to CHOICE is blocked, so the only "way out" for CHOICE is the time-out specified by the /T parameter.
@ECHO OFF IF "%1"=="" GOTO Syntax ECHO. ECHO Waiting %1 seconds ECHO. REM | CHOICE /C:AB /T:A,%1 > NUL IF ERRORLEVEL 255 ECHO Invalid parameter IF ERRORLEVEL 255 GOTO Syntax GOTO End :Syntax ECHO. ECHO WAIT for a specified number of seconds ECHO. ECHO Usage: WAIT n ECHO. ECHO Where: n = the number of seconds to wait (1 to 99) ECHO. :End
Download the Wait.bat source code
Use the SysSleep function whenever you need a time delay in Rexx scripts.
SysSleep is available in OS/2's (native) RexxUtil module and in Patrick McPhee's RegUtil module for 32-bits Windows.
Use the Sleep command for time delays in KiXtart scripts.
Use WScript.Sleep, followed by the delay in milliseconds in VBScript and JScript (unfortunately, this method is not available in HTAs).
The following batch code uses a temporary VBScript file to generate an accurate delay:
@ECHO OFF REM %1 is the number of seconds for the delay, as specified on the command line > "%Temp%.\sleep.vbs" ECHO WScript.Sleep %~1 * 1000 CSCRIPT //NoLogo "%Temp%.\sleep.vbs" DEL "%Temp%.\sleep.vbs"
Or if you want to allow the user to skip the delay:
@ECHO OFF REM %1 is the number of seconds for the delay, as specified on the command line > "%Temp%.\sleep.vbs" ECHO Set wshShell = WScript.CreateObject( "WScript.Shell" ) >> "%Temp%.\sleep.vbs" ECHO ret = wshShell.Popup( "Waiting %~1 seconds", %~1, "Please Wait", vbInformation ) >> "%Temp%.\sleep.vbs" ECHO Set wshShell = Nothing CSCRIPT //NoLogo "%Temp%.\sleep.vbs" DEL "%Temp%.\sleep.vbs"
Compiled versions of SLEEP are also available in these UNIX ports:
|page last uploaded: 11 March 2015, 17:57|