I was working on another batch file today and needed a way for the script to wait for a given number of seconds before continuing to execute. On a whim, I tried the wait command by adding “wait 30“, thinking the script would wait at that spot for 30 seconds before continuing.
The script did wait for 30 seconds, but instead of continuing to execute, it simply exited at that point, leaving the rest of the commands unfinished.
After spending a few minutes trying to get more information about the wait command, I gave up and decided to see what the web had to offer.
There were lots of suggestions, but most of them were of the “twine and bailing wire” approach. Being one to keep things simple, I checked forum after forum until I came upon the timeout command.
The timeout command was introduced in Windows Vista and thankfully is also included in Windows 7. Its one and only purpose is to wait a specified number of seconds – exactly what I was looking for!
Adding the following line will cause your batch file to pause for 30 seconds, then continue on its merry way:
timeout /T 30
As an added bonus, timeout adds a message in the cmd window telling you what its doing and even counts down, telling you how many seconds are left.
timeout /T 30 Waiting for 27 seconds, press a key to continue ...
If you add the /NOBREAK option, random key presses are ignored:
timeout /T 30 /NOBREAK Waiting for 27 seconds, press CTRL+C to quit ...
If you don’t want any message printed out, just redirect the output to nul:
timeout /T 30 /NOBREAK > nul
Kudos to: commandwindows.com for this tip.