Sleeping In A Batch File
Solution 1:
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) oruntil any keyis 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 -1to99999 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 /nobreak
ERROR: Input redirection is not supported, exiting the process immediately.
Solution 2:
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 seconds
PING 1.1.1.1 -n 1 -w 15000 > NUL
or
PING -n 15 -w 1000127.1 >NUL
Solution 3:
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, sys
time.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)
Solution 4:
SLEEP.exe
is included in most Resource Kits e.g. The Windows Server 2003 Resource Kit which can be installed on Windows XP too.
Usage: sleeptime-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)
Solution 5:
I disagree with the answers I found here.
I use the following method entirely based on Windows XP capabilities to do a delay in a batch file:
DELAY.BAT:
@ECHO OFFREM DELAY secondsREM GET ENDING SECONDFOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DOSET /A H=%%A, M=1%%B%%100, S=1%%C%%100, ENDING=(H*60+M)*60+S+%1REM WAIT FOR SUCH A SECOND
:WAIT
FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DOSET /A H=%%A, M=1%%B%%100, S=1%%C%%100, CURRENT=(H*60+M)*60+S
IF %CURRENT% LSS %ENDING% GOTO WAIT
You may also insert the day in the calculation so the method also works when the delay interval pass over midnight.
Post a Comment for "Sleeping In A Batch File"