Sleeping in a batch file Sleeping in a batch file python python

Sleeping in a batch file


The timeout command is available from Windows Vista onwards:

c:\> timeout /?TIMEOUT [/T] timeout [/NOBREAK]Description:    This utility accepts a timeout parameter to wait for the specified    time period (in seconds) or until any key is pressed. It also    accepts a parameter to ignore the key press.Parameter List:    /T        timeout       Specifies the number of seconds to wait.                            Valid range is -1 to 99999 seconds.    /NOBREAK                Ignore key presses and wait specified time.    /?                      Displays this help message.NOTE: A timeout value of -1 means to wait indefinitely for a key press.Examples:    TIMEOUT /?    TIMEOUT /T 10    TIMEOUT /T 300 /NOBREAK    TIMEOUT /T -1

Note: It does not work with input redirection - trivial example:

C:\>echo 1 | timeout /t 1 /nobreakERROR: Input redirection is not supported, exiting the process immediately.


Using the ping method as outlined is how I do it when I can't (or don't want to) add more executables or install any other software.

You should be pinging something that isn't there, and using the -w flag so that it fails after that amount of time, not pinging something that is there (like localhost) -n times. This allows you to handle time less than a second, and I think it's slightly more accurate.

e.g.

(test that 1.1.1.1 isn't taken)

ECHO Waiting 15 secondsPING 1.1.1.1 -n 1 -w 15000 > NUL  orPING -n 15 -w 1000 127.1 >NUL


UPDATE

The timeout command, available from Windows Vista and onwards should be the command used, as described in another answer to this question. What follows here is an old answer.

Old answer

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:

import time, systime.sleep(float(sys.argv[1]))

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 than sleep.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)