WAIT

To make a batch file wait for a number of seconds there are several options available:

Note: Click a 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.

PAUSE

The most obvious way to pause a batch file is of course the PAUSE command. 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.

PING

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 1.1.1.1 -n 1 -w 60000 >NUL

will delay execution of the next command 60 seconds, provided 1.1.1.1 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.

Summarizing: probably the safest, though not the most accurate PING based delay uses 127.0.0.1, and the standard 1 second ping interval instead of a specified time-out interval, i.e. PING 127.0.0.1 -n 6  for a 5 seconds delay.

The PING time-out technique is demonstrated in the following examples:

  Download the PMSleep sources

NETSH

NETSH may seem an unlikely choice to generate delays, but it is actually much like using PING:

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.

CHOICE

Note: The IF ERRORLEVEL 255 ECHO Invalid parameter ends with an "invisible" BELL character, which is ASCII character 7 (beep) or ^G (Ctrl+G).

  Download the Wait.bat source code

Resource Kit

If you own a copy of the Microsoft Windows NT Workstation Resource Kit you don't need to use CHOICE, even though it would probably work.
Use SLEEP for a fixed delay, or TIMEOUT for a pause with a timeout.

Examples:

SLEEP 10

will delay execution of the next command by 10 seconds.

TIMEOUT 10

will delay execution of the next command by 10 seconds, or continue immediately when a key is pressed before those 10 seconds expire.

Non-DOS Scripting

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"

UNIX Ports

Compiled versions of SLEEP are also available in these UNIX ports:

 

 


page last uploaded: 21 April 2012, 18:58