Possible Duplicate:
How to sleep in a batch file?
Here's a haxxorish way to pause for a second in a batch file:
PING 400.500.600.700 > NUL
I've googled but I'm not sure there are any better ones.. any ideas? :)
Possible Duplicate:
How to sleep in a batch file?
Here's a haxxorish way to pause for a second in a batch file:
PING 400.500.600.700 > NUL
I've googled but I'm not sure there are any better ones.. any ideas? :)
The correct way to do this is to use the timeout
command, introduced in Windows 2000.
To wait 30 seconds:
timeout /t 30
The timeout would get interrupted if the user hits any key; however, the command also accepts the optional switch /nobreak
, which effectively ignores anything the user may press, except an explicit CTRL-C
:
timeout /t 30 /nobreak
Additionally, if you don't want the command to print its countdown on the screen, you can redirect its output to NUL
:
timeout /t 30 /nobreak > NUL
You can use the "default choice" and "timeout" options of the built-in choice command to create a delay.
@echo off
echo Hi, I'm doing some stuff
echo OK, now I need to take a breather for 5 seconds...
choice /d y /t 5 > nul
echo Times up! Here I go again...
@echo off
echo It is time for liftoff.
timeout /t 5
echo Commencing crash sequence.
timeout /t 5
Hitting a key will over ride the count down is the only downside.
Here's what I've been using as substitute for sleep.exe,
@ping -n 1 -w 1000 0.0.0.1 > NUL
Change -n x
to wait (roughly) x
seconds.
Apparently the windows resource kit has the sleep command in it. Other Sites also recommend using choice.....
CHOICE command with a timer on it works well...
CHOICE /C:x /T:x,10 > NUL
There are also "programs" out there you could run like WAIT and SLEEP, etc.
Hope this helps.
The ping one (above) is a good one- but only works if connected to a network.
A bit of script that will delay is below:
@echo off
set /a secondsend=%TIME:~6,2%+10
if %secondsend% GTR 59 set /a secondsend=secondsend-60
:waithere
if %TIME:~6,2% NEQ %secondsend% goto waithere
This will pause from between 9-10 seconds (the first second isn't accurate due to using the TIME command- and it could be halfway through a second before you begin).
If works by setting 'secondsend' to the current second of the pc clock, then adding 10 to it (the delay). If it's greater than 59 taking 60 off as it's wrapped around to next minute. Then there is a loop which checks the current second with 'secondsend'- once they match the script continues.
If you want to delay by a different period 2-59 then alter the 10 in the second line (I say 2-59 as the first second might not be a full second, so 2 could be say, 1.2 seconds for example).
Sorry it's so longwinded but thought I'd explain how the routine works.
I just wrote a simple exe in C#. Simply using the statis System.Threading.Sleep, and take the commandline argument and convert it to an integer.
It took 5 minutes to do.