How to sleep for five seconds in a batch file/cmd [duplicate]
Windows's Snipping tool can capture the screen, but sometimes I want to capture the screen after five seconds, such as taking an image being displayed by the webcam. (Run the script and smile at the camera, for example.)
How do I sleep for 5 seconds in a batch file?
I'm very surprised no one has mentioned:
C:\> timeout 5
N.B. Please note however (thanks Dan!) that timeout 5
means:
Sleep anywhere between 4 and 5 seconds
This can be verified empirically by putting the following into a batch file, running it repeatedly and calculating the time differences between the first and second echo
s:
@echo off
echo %time%
timeout 5 > NUL
echo %time%
One hack is to (mis)use the ping command:
ping 127.0.0.1 -n 6 > nul
Explanation:
-
ping
is a system utility that sends ping requests.ping
is available on all versions of Windows. -
127.0.0.1
is the IP address of localhost. This IP address is guaranteed to always resolve, be reachable, and immediately respond to pings. -
-n 6
specifies that there are to be 6 pings. There is a 1s delay between each ping, so for a 5s delay you need to send 6 pings. -
> nul
suppress the output ofping
, by redirecting it tonul
.
Try the Choice command. It's been around since MSDOS 6.0, and should do the trick.
Use the /T parameter to specify the timeout in seconds and the /D parameter to specify the default selection and ignore then selected choice.
The one thing that might be an issue is if the user types one of the choice characters before the timeout period elapses. A partial work-around is to obfuscate the situation -- use the /N argument to hide the list of valid choices and only have 1 character in the set of choices so it will be less likely that the user will type a valid choice before the timeout expires.
Below is the help text on Windows Vista. I think it is the same on XP, but look at the help text on an XP computer to verify.
C:\>CHOICE /?
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout 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.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
The following hack let's you sleep for 5 seconds
ping -n 6 127.0.0.1 > nul
Since ping waits a second between the pings, you have to specify one more than you need.
If you've got PowerShell on your system, you can just execute this command:
powershell -command "Start-Sleep -s 5"
Edit: people raised an issue where the amount of time powershell takes to start is significant compared to how long you're trying to wait for. If the accuracy of the wait time is important (ie a second or two extra delay is not acceptable), you can use this approach:
powershell -command "$sleepUntil = [DateTime]::Parse('%date% %time%').AddSeconds(5); $sleepDuration = $sleepUntil.Subtract((get-date)).TotalMilliseconds; start-sleep -m $sleepDuration"
This takes the time when the windows command was issued, and the powershell script sleeps until 5 seconds after that time. So as long as powershell takes less time to start than your sleep duration, this approach will work (it's around 600ms on my machine).