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.
PING localhost -n 6 >NUL
will delay execution of the next command for (a little over) 5 seconds seconds (default interval between pings is 1 second, the last ping will add only a minimal number of milliseconds to the delay).
So always specify the number of seconds + 1 for the delay.
PING time-out technique is demonstrated in the following examples:
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 localhost, 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 | CHOICE /C:AB /T:A,10 >NUL
will add a 10 seconds delay.
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.
The idea was borrowed from Laurence Soucy, I added the
/C parameter to make it language independent (the simpler
REM | CHOICE /T:N,10 >NUL will work in many but not all languages).
CHOICE delay technique is demonstrated in the following example:
Download the Wait.bat source code
For longer delay times especially, it would be nice to let the user know what time is left.
That is why I wrote CountDown.exe (in C#): it will count down showing the number of seconds left.
Stopping it requires Ctrl+C or Ctrl+Break.
You may append its counter to a custom text, like this:
@ECHO OFF REM Show custom text without linefeed SET /P "=Counting down: " < NUL CountDown 10
Download CountDown.exe and its C# 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 = 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: 2017-09-01, 14:44|