up vote 8 down vote favorite
2
share [g+] share [fb]

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

link|improve this question
feedback

6 Answers

up vote 12 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...
link|improve this answer
2  
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
AFAIK, 'choice' is not included with any NT-based Windows OS. – grawity Jul 10 '09 at 12:12
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. – Graeme Donaldson Jul 12 '09 at 11:04
timeout also is only available from Vista onwards. – Joey Feb 5 '11 at 0:19
There are a few ideas on addressing the problem of the user pressing keys at erichelps. – romkyns May 30 '11 at 9:13
feedback

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.

link|improve this answer
It waits x − 1 seconds. – Joey Feb 5 '11 at 0:19
feedback

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

link|improve this answer
feedback

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.

link|improve this answer
feedback

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.

link|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. – Joey Feb 5 '11 at 0:21
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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