How to wait/sleep in a batch script

Quoted from: http://stackoverflow.com/questions/735285/how-to-wait-in-a-batch-script

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

ping 192.0.2.2 -n 1 -w 10000 > nul

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

  • The -w 10000 part specifies the desired timeout in milliseconds.
  • The -n 1 part tells ping that it should only tries 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 use the above technique:

rem SLEEP.BAT - sleeps by the supplied number of seconds
@ping 192.0.2.2 -n 1 -w %1000 > nul

NOTE: 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.

posted @ 2014-04-09 15:21  IT麦兜  阅读(125)  评论(0)    收藏  举报