How to Put Delay / Sleep in Batch Files

By: subhash chandra
February 15, 2011

Hi Guys,

Today I'm gonna post something on doing some delay /wait time in batch files.We have seen many people’s asking help as to how they can  put some sleep time between commands within a batch file.  Unfortunately there is no such built in commands available to accomplish this in windows ,however using some workarounds we can do. Usually when I started with batch files I though there is no such thing to do at least from command prompt, later I came to know few ways.

First of all, If you want the delay just  to allow some other program to finish then the best thing to use Start /w option.

i) Start /w

START /W program.exe

program.exe means the program executable you want to execute. The batch file will wait until the program terminates. The advantage is it won’t use any extra CPU/Memory while waiting.

ii) Using Ping command 

There are 2 ways to use ping (may be others as well which i am not aware)

Ping -n and ping -w

The ping –n parameter pings the given ip for n number of time. We assume it takes one second to ping once. But that is not the case always. If exact timing is not required then we can use this option. The idea is to ping your computer itself (localhost ,IP n number of times. So its , Ping –n 30 it will wait for 30 secs approx before going to next line of code. But it will produce the ping output to the console. To hide this use the redirector to redirect the output to null device.

Ping –n 30 >null

The disadvantage using this method is ping response depends on various bits, like the network card performance, computer speed, available memory so -30 does not means it will use exact 30 secs. On a modern faster computer it can take about 20 secs or on a old PIII computer it can take more than 30.

So, lets try another work around. The ping command comes with a parameter –w .

Ping –w 6000

-w parameter tells ping to wait until specified time(In millisecond)  before it says request timeout or not reachable.The idea is to ping an non-exist IP and tell it to wait for the time we want. Like in above example I m pinging which does not exist as per my understating (Check yourself before implening and If it replies use another IP). I asked it to wait for 6000 milisecs (6 Secs) and it does wait exactly for 6 seconds. So its even better than the –n option, make sure to use the redirector to redirect output to nul.

iii) The choice command.

Windows vista and letter comes with a command Choice. CHOICE has the "/t" option which lets it automatically select an option for you after a time delay. Actually This tool allows users to select one item from a list of choices and returns the index of the selected choice. The number of seconds to pause before a default  choice is made. Acceptable values are from 0 to  9999. If 0 is specified, there will be no pause  and the default choice is selected. Try this:

choice /t 3  /d Y >null

The command prompt will wait for 3 seconds before it comes next.

/t switch tells it to wait till specified seconds and /d tells it to chose Y as a default choice , We don’t have to do anything with the default choice here. Choice can be used as an alternative to the set /p command in case you want a menu to be presented to console to choose an option. Choice has the limitation that it is not available in windows xp/2003 and it will terminate if you press any key while it is running.

Alright.. We have seen 3 methods above, the little problem with them is that they use lot of CPU time while execution. Lets see a way in which we can keep waiting without loosing CPU time/power/resourses and my fav one.

iv) Using vbscript to do the sleep

Windows Scripting host (WSH) sleep will use very less CPU and can be used for long delays as well.  It’s a built in feature and the VBS file can be created over the file from the existing batch file in simple to understand code. Let’s see how we can accomplish this. I m putting explanation in braces.

@echo off (Our normal batch code)
echo Wscript.Sleep 10000> %temp%\sleep.vbs   (add this line below echo off or anywhere before you want sleep)
start /w wscript.exe %temp%\sleep.vbs (put this line where you want sleep)
del %temp%\sleep.vbs   (Put this in the end to cleanup J )

Isn’t that simple enough. Note that the value 10000 is in millisecond. It accepts milliseconds value. This will not work if you don’t have write access to target (Temp) directory ,it happned in rare case where your user wont have access to the temp folder itself.

So..thats all for today>hope you enjoyed.

Thank you.
Subhah Chandra

Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question