When writing a batch file to automate something on a Windows box, I've needed to pause its execution for several seconds (usually in a test/wait loop, waiting for a process to start). At the time, the best solution I could find uses ping (I kid you not) to achieve the desired effect. I've found a better write-up of it here, which describes a callable "wait.bat", implemented as follows:
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul
You can then include calls to wait.bat in your own batch file, passing in the number of seconds to sleep.
Apparently the Windows 2003 Resource Kit provides a Unix-like sleep command (at last!). In the meantime, for those of us still using Windows XP, Windows 2000 or (sadly) Windows NT, is there a better way?
I modified the sleep.py
script in the accepted answer, so that it defaults to one second if no arguments are passed on the command line:
import time, sys
time.sleep(float(sys.argv[1]) if len(sys.argv) > 1 else 1)
For example, to make the script perform a non-uninterruptible 2-second wait:
Which means the script will wait 2 seconds before continuing.
By default, a keystroke will interrupt the timeout, so use the
/nobreak
switch if you don't want the user to be able to interrupt (cancel) the wait. Furthermore, the timeout will provide per-second notifications to notify the user how long is left to wait; this can be removed by piping the command toNUL
.edit: As @martineau points out in the comments, the
timeout
command is only available on Windows 7 and above. Furthermore, theping
command uses less processor time thantimeout
. I still believe in usingtimeout
where possible, though, as it is more readable than theping
'hack'. Read more here.If you have Python installed, or don't mind installing it (it has other uses too :), just create the following sleep.py script and add it somewhere in your PATH:
It will allow sub-second pauses (for example, 1.5 sec, 0.1, etc.), should you have such a need. If you want to call it as
sleep
rather thansleep.py
, then you can add the.PY
extension to your PATHEXT environment variable. On Windows XP, you can edit it in:My Computer → Properties (menu) → Advanced (tab) → Environment Variables (button) → System variables (frame)
There is a better way to sleep using ping. You'll want to ping an address that does not exist, so you can specify a timeout with millisecond precision. Luckily, such an address is defined in a standard (RFC 3330), and it is
192.0.2.x
. This is not made-up, it really is an address with the sole purpose of not-existing (it may not be clear, but it applies even in local networks):To sleep for 123 milliseconds, use
ping 192.0.2.1 -n 1 -w 123 >nul
From Windows Vista on you have the TIMEOUT and SLEEP commands, but to use them on Windows XP or Windows Server 2003, you'll need the Windows Server 2003 resource tool kit.
Here you have a good overview of sleep alternatives (the ping approach is the most popular as it will work on every Windows machine), but there's (at least) one not mentioned which (ab)uses the
W32TM
(Time Service) command:Where you should replace the N with the seconds you want to pause. Also, it will work on every Windows system without prerequisites.
Typeperf can also be used:
Just put this in your batch file where you want the wait.
Replace X with the number of seconds + 1.
For example, if you were to wait 10 seconds, replace X with 11. To wait 5 seconds, use 6.
Read earlier answers for milliseconds.