193

I am trying to write a batch script and trying to wait 10 seconds between 2 function calls. The command:

sleep 10

Does not make the batch file wait for 10 seconds.

I am running Windows XP.

Note: This is not a complete duplicate of Sleeping in a batch file as the other question is about also about python, while this is about windows batch files.

2

6 Answers 6

288

You can ping an address that doesn't exist and specify the desired timeout:

ping 192.0.2.2 -n 1 -w 10000 > nul

And since the address does not exist, it'll wait 10,000 ms (10 seconds) and return.

  • The -w 10000 part specifies the desired timeout in milliseconds.
  • The -n 1 part tells ping that it should only try once (normally it'd try 4 times).
  • The > nul part is appended so the ping command doesn't output anything to screen.

You can easily make a sleep command yourself by creating a sleep.bat somewhere in your PATH and using the above technique:

rem SLEEP.BAT - sleeps by the supplied number of seconds

@ping 192.0.2.2 -n 1 -w %1000 > nul

NOTE (September 2020): The 192.0.2.x address is reserved as per RFC 3330 so it definitely will not exist in the real world. Quoting from the spec:

192.0.2.0/24 - 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.

8
  • 2
    ping 127.0.0.1 -n 5 -w 1000 > nul hat to do it like this since the other would finish right away. Commented Apr 9, 2009 at 18:58
  • @Thomaschaaf interesting... pinging a local machine address should've caused ping to return right away since it's pinging the same machine (i.e. time<1ms) so the timeout trick shouldn't have worked in that case!
    – chakrit
    Commented Apr 9, 2009 at 19:04
  • 16
    I added the actual function its called timeout.. ss64.com/nt/timeout.html at least I know that others didn't know either :) Commented Apr 9, 2009 at 19:42
  • 3
    doesn't work, ping responds with: PING: transmit failed. General failure. probably because I'm in a 10.x.x.x range. Commented May 28, 2013 at 7:13
  • 2
    I was happily using ping 0.0.0.0 -n 1 -w 10000 > nul until Windows 10 which will immediately return with this particular IP, without any waiting. @ping 1.0.0.0 -n 1 -w 3000 > nul works. Commented Aug 21, 2015 at 13:36
151

You'd better ping 127.0.0.1. Windows ping pauses for one second between pings so you if you want to sleep for 10 seconds, use

ping -n 11 127.0.0.1 > nul

This way you don't need to worry about unexpected early returns (say, there's no default route and the 123.45.67.89 is instantly known to be unreachable.)

3
  • 27
    I like this solution more than the the one marked as the answer because this is cleaner as we don't have to make sure that our script has a non-existent IP address. Therefore, we can write the script once and use it on any Windows system in any network without editing the script again.
    – Susam Pal
    Commented Jul 31, 2012 at 11:34
  • 4
    Since RFC 3330 became obsoleted I'm using this solution.
    – Reyno
    Commented Sep 19, 2013 at 7:58
  • 4
    Or perhaps use localhost to avoid problems on IPv6-only machines. Commented Oct 24, 2014 at 5:14
142

I actually found the right command to use.. its called timeout: http://www.ss64.com/nt/timeout.html

6
  • 2
    alternative would be chakrits ss64.com/nt/sleep.html Commented Apr 9, 2009 at 19:44
  • 8
    Also not a Windows XP command... Commented Apr 10, 2009 at 0:10
  • 4
    Now that WindowsXP is on the way out, it seems like this is really the way to do it in the future.
    – djangofan
    Commented Feb 7, 2013 at 22:18
  • 8
    This may not have been the correct answer for XP in 2009 but it is for Windows 7 and above!
    – Kristopher
    Commented Aug 29, 2013 at 12:17
  • 5
    In 2016, this should become the correct answer.
    – TTT
    Commented Mar 31, 2016 at 19:51
49

I used this

:top
cls
type G:\empty.txt
type I:\empty.txt
timeout /T 500
goto top
4
  • 1
    timeout is nice solution. Its in fact better than sleep. It tells time remaining!
    – Manish
    Commented Jan 24, 2011 at 10:01
  • 8
    timeout command isn't available on Windows XP. It works on Windows 2003 and Windows 7 though.
    – Susam Pal
    Commented Jul 31, 2012 at 11:32
  • 3
    seconds? milliseconds? minutes? what is the unit that it uses?
    – Itay Levin
    Commented Aug 11, 2014 at 14:36
  • 1
    @ItayLevin: the unit is seconds.
    – A.L
    Commented Oct 24, 2014 at 11:15
7

What about:

@echo off
set wait=%1
echo waiting %wait% s
echo wscript.sleep %wait%000 > wait.vbs
wscript.exe wait.vbs
del wait.vbs
1
  • 2
    This is the only solution that seems to be accurate even for delays that are <1000ms.
    – lynn
    Commented Jul 20, 2015 at 17:19
6

Well, does sleep even exist on your Windows XP box? According to this post: http://malektips.com/xp_dos_0002.html sleep isn't available on Windows XP, and you have to download the Windows 2003 Resource Kit in order to get it.

Chakrit's answer gives you another way to pause, too.

Try running sleep 10 from a command prompt.

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