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

Srekel
  • 359
  • 2
  • 3
  • 7
  • 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
  • This question was referenced in Stack Overflow question *[Sleeping in a batch file](https://stackoverflow.com/questions/166044/sleeping-in-a-batch-file/1811248#1811248)* – Peter Mortensen Aug 16 '19 at 11:46

8 Answers8

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
Massimo
  • 68,714
  • 56
  • 196
  • 319
  • 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 07: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 08: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 '12 at 09:40
  • @Joey, I tested it *right now* on a 2003 box with no resource kit or support tools installed. It works perfectly. – Massimo Sep 27 '12 at 09:51
  • Note that the article says “Windows *Server* 2000”; that's not Windows 2000 and the server OSes may indeed have this. Not the consumer ones before Vista, though (and Vista mybe only because its starting point was the Server 2k3 codebase). – Joey Sep 27 '12 at 10:25
  • The problem I have with timeout is that it squelches any following commands when the timeout period elapses, so those commands are not run. – Tola Odejayi Jul 12 '16 at 00:11
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...
ThatGraemeGuy
  • 15,314
  • 12
  • 51
  • 78
  • 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. – Richard West Jul 09 '09 at 16:53
  • 1
    AFAIK, 'choice' is not included with any NT-based Windows OS. – user1686 Jul 10 '09 at 12:12
  • 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. – ThatGraemeGuy Jul 12 '09 at 11:04
  • 4
    `timeout` also is only available from Vista onwards. – Joey Feb 05 '11 at 00:19
  • There are a few ideas on addressing the problem of the user pressing keys [at erichelps](http://www.ericphelps.com/batch/samples/sleep.txt). – RomanSt May 30 '11 at 09:13
  • 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
  • 3
    @Joey, the command was actually introduced in Windows 2000. – Massimo Sep 26 '12 at 19:12
  • Massimo: Only if you installed a resource kit, I guess. Not natively included. – Joey Sep 26 '12 at 20:06
  • @Joey, the Technet article I linked says Windows 2000, but I indeed have no 2000 box at hand to confirm this; however, I know for sure it's natively available in XP/2003. – Massimo Sep 27 '12 at 07:05
  • 1
    @Massimo not quite - 2003 does have it, XP/2000 does not. – the-wabbit Sep 27 '12 at 07:28
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.

Frederik
  • 3,293
  • 3
  • 30
  • 46
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.

nray
  • 1,540
  • 17
  • 23
  • 1
    It waits *x* − 1 seconds. – Joey Feb 05 '11 at 00:19
  • 5
    Using `ping` to pause batch execution is definitely more appropriate for http://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. – Joey Sep 26 '12 at 20:07
2

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

James
  • 2,212
  • 1
  • 13
  • 19
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.

KPWINC
  • 11,274
  • 3
  • 36
  • 44
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.

  • 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 08: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. –  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 05 '11 at 00: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.