15

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

2
  • 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, 2012 at 19:10
  • This question was referenced in Stack Overflow question Sleeping in a batch file Aug 16, 2019 at 11:46

8 Answers 8

12

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...
10
  • 6
    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. Jul 9, 2009 at 16:53
  • 2
    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. Jul 12, 2009 at 11:04
  • 4
    timeout also is only available from Vista onwards.
    – Joey
    Feb 5, 2011 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, 2012 at 19:11
  • 3
    @Joey, the command was actually introduced in Windows 2000.
    – Massimo
    Sep 26, 2012 at 19:12
32

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
8
  • 1
    I think this is the best way as it is designed to pause, unlike the ping program.
    – jftuga
    Sep 26, 2012 at 19:16
  • And also unlike choice...
    – Massimo
    Sep 26, 2012 at 19:19
  • 1
    The trouble is, it is not included with a stock install of Windows before Vista.
    – the-wabbit
    Sep 27, 2012 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, 2012 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.
    – Joey
    Sep 27, 2012 at 9:40
4
@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.

1
  • 2
    Add the /nobreak option to have it ignore keypresses.
    – Starfish
    Sep 26, 2012 at 19:23
3

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.

3
  • 1
    It waits x − 1 seconds.
    – Joey
    Feb 5, 2011 at 0:19
  • 5
    Using ping to pause batch execution is definitely more appropriate for thedailywtf.com than ServerFault...
    – Massimo
    Sep 26, 2012 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.
    – Joey
    Sep 26, 2012 at 20:07
2

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

1

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.

1

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.

3
  • 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, 2009 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, 2009 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, 2011 at 0:21
-1

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.

Not the answer you're looking for? Browse other questions tagged .