How to pause execution for a while in a Windows batch file between a command and the next one?

  • Is this intended to become a canonical? If so then some explanatory text might be appropriate. – John Gardeniers Sep 26 '12 at 23:06
  • 2
    "Explanatory text" as in? – Massimo Sep 27 '12 at 7:00
  • 2
    Most, if not all, canonical questions I've seen include text describing the intent of that question. – John Gardeniers Sep 27 '12 at 8:52
  • Or some text explaining the situation... It's mostly to help with SEO, but also helps to frame potential Answers. – Chris S Sep 28 '12 at 1:12
  • 2
    SEO working just fine. First hit for me on Google search. – jbo5112 Oct 1 '15 at 22:44

The correct way to sleep in a batch file is to use the timeout command, introduced in Windows 2000.

To wait somewhere between 29 and 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
  • 5
    The timeout command does not work with Windows XP even with the 2003 Resource kit installed and the sleep command does not work in Windows 7 so the ping command can still be of good use if you are using the batch file on both Win7 and WinXP. – user178627 Jun 20 '13 at 22:55
  • 9
    timeout does not work in non-interactive scripts: "ERROR: Input redirection is not supported, exiting the process immediately." so sometimes using hack with "ping" is preferred. – iMysak Oct 26 '15 at 11:08

Since it applies here, too, I'll copy my answer from another site.

If you want to use ping, there is a better way. You'll want to ping an address that does not exist, so you can specify a timeout with millisecond precision. Luckily, such an address is defined in a standard (RFC 3330), and it is 192.0.2.x. This is not made-up, it really is an address with the sole purpose of not-existing (it may not be clear, but it applies even in local networks): - This block is assigned as "TEST-NET" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. Addresses within this block should not appear on the public Internet.

To sleep for 123 milliseconds, use ping -n 1 -w 123 >nul

  • 2
    Please don't do that, even if it's slightly better than pinging a real address. Ping is simply the wrong tool for the job. – Massimo Jul 17 '14 at 12:39
  • 1
    @Massimo It's most likely not what it was intended to do :) But I think it's a good solution still - it does work better than timeout in some scenarios (in particular higher resolution). Also, it's become quite well known, so most people would not be very surprised (i.e. confused) when they see it in your batch file. – mafu Jul 18 '14 at 8:15
  • This QA was created exactly to point out the right solution to the problem, because there are several other similar questions which were answered with "just use ping" or similar kludges. – Massimo Jul 18 '14 at 11:15
  • 2
    @Massimo Alright! I guess we just have different views. If you don't mind I'll still leave this answer up since it seems to be better than pinging localhost n times as is most often described. – mafu Jul 18 '14 at 11:25
  • 7
    timeout is the right solution for the operating systems it works on. ping is the only solution that works from 5.1-6.3. With 25% of systems still on 5.1, timeout is not "the correct way". – Wyrmwood Jul 23 '14 at 1:42

You can also insert a ping to localhost. This will take 4 seconds to complete (by default). It is considered a kludge by some, but works quite well all the same.

The command:

  • 6
    Ehm... the whole purpose of this Q/A was exactly to correct two other questions which have this as their accepted answer, but can no longer be corrected because their OP is no longer around. – Massimo Sep 26 '12 at 20:13
  • 6
    While it will work it's the equivalent of driving a screw with a hammer. A wrong tool may work, but it's still wrong. – voretaq7 Sep 26 '12 at 20:16
  • 1
    @MarkAllen In other words, Windows NT4, 95, 98, ME and older? – Chris McKeown Sep 26 '12 at 23:44
  • 5
    @MarkAllen - technically you then have to worry about whether or not ping comes with your OS. If you're worried enough about '95 '98 or ME then you should be as equally worried about 6.22 which doesn't have ping either – Mark Henderson Sep 27 '12 at 20:36
  • 4
    @MarkHenderson It's easier (for me) to remember whether or not a particular release of the OS supports TCP/IP than to remember whether or not it includes a command I hadn't heard previously. You know? – Mark Allen Sep 28 '12 at 0:01

protected by Michael Hampton Dec 2 '15 at 22:31

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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