Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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? :)

share|improve this question

marked as duplicate by Massimo, MDMarra, Mark Henderson Sep 26 '12 at 20:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Silly that the command would even be accepted... that's in no way a valid IP address. But apparently, the Windows ping command treats anything shorter than four digits as an acceptable IP octet... –  Massimo Sep 26 '12 at 19:10
add comment

8 Answers

up vote 7 down vote accepted

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...
share|improve this answer
4  
It should be noted that while the choice command is waiting 5 seconds for input, the timer gets reset each and every time the user presses a key. So it is possible that your batch file will never exit this choice step. Therefore it is also impossible to guarantee that the routine will only wait 5 seconds -- it could be longer because of a key press. –  Richard West Jul 9 '09 at 16:53
1  
At first I thought you were wrong. I'm using Vista and 'choice' works just fine, but upon further investigation it seems that 2000/XP/2003 didn't have it by default. For some reason they brought it back with Vista/2008. –  ThatGraemeGuy Jul 12 '09 at 11:04
3  
timeout also is only available from Vista onwards. –  Јοеу Feb 5 '11 at 0:19
3  
Sorry for the downvote, but that's just another kludge, and timeout is there exactly for this purpose. –  Massimo Sep 26 '12 at 19:11
1  
@Joey, the command was actually introduced in Windows 2000. –  Massimo Sep 26 '12 at 19:12
show 5 more comments

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
share|improve this answer
1  
I think this is the best way as it is designed to pause, unlike the ping program. –  jftuga Sep 26 '12 at 19:16
    
And also unlike choice... –  Massimo Sep 26 '12 at 19:19
1  
The trouble is, it is not included with a stock install of Windows before Vista. –  the-wabbit Sep 27 '12 at 7:23
    
Wrong. I know for personal experience that at least XP/2003 have it natively. Technet says Windows 2000 too, but this I don't know for sure. –  Massimo Sep 27 '12 at 8:52
    
Massimo, you likely have the Server 2003 Resource Kit or similar installed which comes with a few utilities that shipped by default on later versions. –  Јοеу Sep 27 '12 at 9:40
show 2 more comments
@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.

share|improve this answer
2  
Add the /nobreak option to have it ignore keypresses. –  Starfish Sep 26 '12 at 19:23
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
Nice. Beware, though, that this is a tight look which might consume much of the processing time on the core it's running on, right? –  bzlm Nov 17 '09 at 8:05
    
Yes you're right bzlm- much of the spare capacity of the CPU seems to get used up, though other programs can still run ok and 'take some back'. Just been testing it. Shame that. I wonder if anything can be inserted into the loop to avoid this? This is the beauty of the proper 'sleep' command, it just counts clock cyces and does not hog the cpu processes, allowing better resources use. Pity 'sleep' isn't a built in function. Mark H. –  Mark H Nov 17 '09 at 20:06
    
I have yet to see a recent version of Windows that didn't have at least IPv4 installed. ping -n 6 127.0.0.1 works fine to sleep 5 seconds, even if no network interface has a connection. –  Јοеу Feb 5 '11 at 0:21
add comment

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.

share|improve this answer
    
It waits x − 1 seconds. –  Јοеу Feb 5 '11 at 0:19
4  
Using ping to pause batch execution is definitely more appropriate for thedailywtf.com than ServerFault... –  Massimo Sep 26 '12 at 19:33
2  
Massimo, it's a solution that works on pretty much any Windows without resorting to tools that may not exist on older versions. –  Јοеу Sep 26 '12 at 20:07
add comment

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.

share|improve this answer
add comment

Apparently the windows resource kit has the sleep command in it. Other Sites also recommend using choice.....

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.