The command timeout is the best choice for waiting a specific time in a batch file which is designed for execution on Windows 7 and later versions of Windows. It supports breaking the timeout by the user with any key except /NOBREAK
is specified as parameter. And it shows a nice message with a seconds countdown for the user informing also the user how to break the timeout. But it supports only timeout values in seconds, not in milliseconds.
The command sleep could be also used on Windows XP and later versions of Windows when having access to Windows 2003 resource kit and this small executable is copied to all computers running the batch file. But this executable is deprecated because of being replaced by TIMEOUT and by default not installed on any Windows computer.
But a good choice for all Windows is using the command ping for pinging the loopback adapter or a not reachable IP address with using appropriate values of the options -n
and -w
for the delay.
The IP address of the loopback adapter of local machine is 127.0.0.1
, see Wikipedia articles about Reserved IP addresses. localhost
is just an RFC defined alias for 127.0.0.1
defined on Windows XP and former Windows versions in file %SystemRoot%\system32\drivers\etc\hosts
and is defined built-in on Windows Vista and later Windows versions.
The first ping of 127.0.0.1
is always immediately successful. Therefore using command PING with -n 1
as option gives just a delay of approximately a millisecond in total.
For that reason using PING as delay on pinging 127.0.0.1
requires a value greater 1
for option -n
... number of echo requests to send. After a successful request PING waits about 1 second before making the next request.
So for a delay of 5
seconds the following command line is necessary with 6
echo requests:
%SystemRoot%\System32\ping.exe 127.0.0.1 -n 6 >nul
Note 1: Windows is not a real-time operating system and for that reason the time is not 100% accurate, but should be good enough for a batch file.
The option -w
defines in milliseconds how long ping (Microsoft documentation) waits for an echo on the request. It does not define the time between two successful requests. Therefore this option can't be used to fine tune the delay on pinging the IP address 127.0.0.1
as this request is successful in less than 1 millisecond and value of option -w
does not matter.
So for a delay in milliseconds instead of seconds it is necessary to ping an IP address which is definitely or at least most likely not reachable and which is not routed via networks because of being a private network address according to RFC 6761.
An example is:
%SystemRoot%\System32\ping.exe 168.192.255.253 -n 1 -w 1500
The IPv4 address range from 168.192.0.0 to 192.168.255.255 is for private networks. The highest address 192.168.255.255 in this network is the broadcast address and is not used for devices. It is common to configure a router with local area network broadcast address minus 1 which means 192.168.255.254 could be assigned to a router in case of current computer is part of this private network. And other devices in a LAN get assigned usually the IPv4 addresses from lowest address plus 1 upwards. Therefore for IPv4 network 168.192.0.0/16 the IP address 168.192.255.253 is most likely not assigned to any device which would respond on the echo request of PING.
Well, the milliseconds delay is not very accurate. But is it really important on execution of a batch file to wait exactly 1500 ms?
Note 2: This approach does not work if the computer on which the batch file is running is currently not connected to any network. Without any network connection each echo request is always immediately terminated and PING outputs for each echo request the error message:
PING: transmit failed. General failure.
The general failure is no network (connection) present at all and therefore only echo requests to local loopback adapter work.