batch How to sleep for 5 seconds in Windows's Command Prompt?(or DOS)
windows timeout command (24)
This question already has an answer here:
Windows's Snipping tool can capture the screen, but sometimes I want to capture the screen after 5 seconds, such as taking an image being displayed by the webcam. (run the script and smile at the camera, for example).
So in Ruby, I could do something like
sleep 3 system('c:/windows/system32/SnippingTool.exe')
but not all computer has Ruby, so how do I do that in a .bat file? (something that is runnable on most PC with Snipping tool).
The problem is that there is no "sleep" usable in a .bat file.
I made this. It is working and show time left in seconds. If you want to use it, add to a batch file:
call wait 10
It was working when I tested it.
wait.bat (it must be in the working directory or
@echo off set SW=00 set SW2=00 set /a Sec=%1-1 set il=00 @echo Wait %1 second for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TBASE=((HH*60+MM)*60+SS)*100+CC, SW=CC set /a TFIN=%TBASE%+%100 :ESPERAR for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TACTUAL=((HH*60+MM)*60+SS)*100+CC, SW2=CC if %SW2% neq %SW% goto notype if %il%==0 (echo Left %Sec% second & set /a Sec=sec-1 & set /a il=il+1) goto no0 :notype set /a il=0 :no0 if %TACTUAL% lss %TBASE% set /a TACTUAL=%TBASE%+%TACTUAL% if %TACTUAL% lss %TFIN% goto ESPERAR
Firstly, to delay in a batch file, simply without all the obtuse methods people have been proposing:
timeout /t <TimeoutInSeconds> [/nobreak]
Secondly, worth mentioning that while it may not do exactly what you want, using the inbuilt Windows snipping tool, you can trigger a snip on it without using the mouse. Run the snipping tool, escape out of the current snip but leave the tool running, and hit Control + Print Screen when you want the snip to occur. This shouldn't interfere with whatever it is you're trying to snip.
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).
Make a cmd file called sleep.cmd:
REM Usage: SLEEP Time_in_MiliSECONDS @ECHO off ping 220.127.116.11 -n 1 -w %1 > nul
Copy sleep.cmd to c:\windows\system32
Sleeps for 0.5 seconds. Arguments in ms. Once copied to System32, can be used everywhere.
EDIT: You should also be away that if the machine isn't connected to a network (say a portable that your using in the subway), the ping trick doesn't really work anymore.
It can be done with two simple lines in a batch file: write a temporary
.vbs file in the
%temp% folder and call it:
echo WScript.Sleep(5000) >"%temp%\sleep.vbs" cscript "%temp%\sleep.vbs"
Well this works if you have choice or ping.
@echo off echo. if "%1"=="" goto askq if "%1"=="/?" goto help if /i "%1"=="/h" goto help if %1 GTR 0 if %1 LEQ 9999 if /i "%2"=="/q" set ans1=%1& goto quiet if %1 GTR 0 if %1 LEQ 9999 set ans1=%1& goto breakout if %1 LEQ 0 echo %1 is not a valid number & goto help if not "%1"=="" echo.&echo "%1" is a bad parameter & goto help goto end :help echo SLEEP runs interactively (by itself) or with parameters (sleep # /q ) echo where # is in seconds, ranges from 1 - 9999 echo Use optional parameter /q to suppress standard output echo or type /h or /? for this help file echo. goto end :askq set /p ans1=How many seconds to sleep? ^<1-9999^> echo. if "%ans1%"=="" goto askq if %ans1% GTR 0 if %ans1% LEQ 9999 goto breakout goto askq :quiet choice /n /t %ans1% /d n > nul if errorlevel 1 ping 18.104.22.168 -n 1 -w %ans1%000 > nul goto end :breakout choice /n /t %ans1% /d n > nul if errorlevel 1 ping 22.214.171.124 -n 1 -w %ans1%000 > nul echo Slept %ans1% second^(s^) echo. :end
just name it sleep.cmd or sleep.bat and run it
rem *** HACK ALERT: Sleep for 5 seconds *** ping -n 6 127.0.0.1 > nul rem ***************************************
- Fixed as explained by @Joey
I wrote a powerbasic program wait.exe, where you pass a millisecond parameter to it in your batch file
wait 3000 system('c:/windows/system32/SnippingTool.exe')
the code for the EXE:
FUNCTION PBMAIN() c$ = Command$ s! = Val(c$)*1000 Sleep s! END FUNCTION
Timeout /t 1 >nul
Is like pause in 1 secound, you can take the limed to almost 100.000 (99.999) secounds. If you are connected to the internet the best solution would be:
ping 126.96.36.199. -n 1 -w 1000 >nul
When you ping you count in milliseconds, so one second would be 1000 milliseconds. But the ping command is a little iffy, it does not work the same way on offline machines.
The problem is that the machine gets confused because it is offline, and it would like to ping a
website/server/host/ip, but it can't.
So i would recommend timeout.
This is the latest version of what I am using in practice for a ten second pause to see the output when a script finishes.
BEST>@echo done BEST>@set DelayInSeconds=10 BEST>@rem Use ping to wait BEST>@ping 192.0.2.0 -n 1 -w %DelayInSeconds%000 > nul
The echo done allows me to see when the script finished and the ping provides the delay. The extra @ signs mean that I see the "done" text and the waiting occurs without me being distracted by their commands.
I have tried the various solutions given here on an XP machine, since the idea was to have a batch file that would run on a variety of machines, and so I picked the XP machine as the environment likely to be the least capable.
GOOD> ping 192.0.2.0 -n 1 -w 3000 > nul
This seemed to give a three second delay as expected. One ping attempt lasting a specified 3 seconds.
BAD> ping -n 5 192.0.2.0 > nul
This took around 10 seconds (not 5). My explanation is that there are 5 ping attempts, each about a second apart, making 4 seconds. And each ping attempt probably lasted around a second making an estimated 9 seconds in total.
BAD> timeout 5 BAD> sleep /w2000 BAD> waitfor /T 180 BAD> choice
Commands not available.
BAD> ping 192.0.2.0 -n 1 -w 10000 > nul :: wait 10000 milliseconds, ie 10 secs
I tried the above too, after reading that comments could be added to BAT files by using two consecutive colons. However the software returned almost instantly. Putting the comment on its own line before the ping worked fine.
GOOD> :: wait 10000 milliseconds, ie 10 secs GOOD> ping 192.0.2.0 -n 1 -w 10000 > nul
To understand better what ping does in practice, I ran
ping 192.0.2.0 -n 5 -w 5000
This took around 30 seconds, even though 5*5=25. My explanation is that there are 5 ping attempts each lasting 5 seconds, but there is about a 1 second time delay between ping attempts: there is after all little reason to expect a different result if you ping again immediately and it is better to give a network a little time to recover from whatever problem it has had.
Edit: stolen from another post, .. RFC 3330 says the IP address 192.0.2.0 should not appear on the internet, so pinging this address prevents these tests spamming anyone! I have modified the text above accordingly!
By using "ping" the -n will determine the timeout only when there is no response to the ping. Check out this post about implementing DELAY as a batch file.
I could just copy-paste the important bits, but the whole post is quite useful.
One hack is to (mis)use the ping command:
ping 127.0.0.1 -n 6 > nul
pingis a system utility that sends ping requests.
pingis available on all versions of Windows.
127.0.0.1is the IP address of localhost. This IP address is guaranteed to always resolve, be reachable, and immediately respond to pings.
-n 6specifies 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.
> nulsuppress the output of
ping, by redirecting it to
ping waits for about 5 seconds before timing out, not 1 second as was stated above. That is, unless you tell it to only wait for 1 second before timing out.
ping 188.8.131.52 -n 1 -w 1000
will ping once, wait only 1 second (1000 ms) for a response, then time out.
So an approximately 20-second delay would be:
ping 184.108.40.206 -n 20 -w 1000
I use the following method entirely based on Windows XP capabilities to do a delay in a batch file:
@ECHO OFF REM DELAY seconds REM GET ENDING SECOND FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, ENDING=(H*60+M)*60+S+%1 REM WAIT FOR SUCH A SECOND :WAIT FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, CURRENT=(H*60+M)*60+S IF %CURRENT% LSS %ENDING% GOTO WAIT
You may also insert the day in the calculation so the method also works when the delay interval pass over midnight.
Can't we do
waitfor /T 180?
waitfor /T 180 pause will result in "ERROR: Timed out waiting for 'pause'."
waitfor /T 180 pause >nul will sweep that "error" under the rug
waitfor command should be there in Windows OS after Win95
In the past I've downloaded a executable named
sleep that will work on the command line after you put it in your path.
sleep shutdown -r -f /m \\yourmachine
although shutdown now has -t option built in
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."
two more ways that should work on everything from XP and above:
w32tm /stripchart /computer:localhost /period:5 /dataonly /samples:2 1>nul
typeperf "\System\Processor Queue Length" -si 5 -sc 1 >nul
You can use VBScript, for example, file
set wsobject = wscript.createobject("wscript.shell") do while 1=1 wsobject.run "SnippingTool.exe",0,TRUE wscript.sleep 3000 loop
cscript myscript.vbs %1
This should do the trick: choice /T 5 /N /D Y
Replace the 5 with the time in seconds you want to wait...
An improvement of the code proposed by the user Aacini, It has resolution of hundredths of a second and does not fail when the time reaches 23:59:59,99:
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TBASE=((HH*60+MM)*60+SS)*100+CC :: Example delay 1 seg. set /a TFIN=%TBASE%+100 :ESPERAR for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TACTUAL=((HH*60+MM)*60+SS)*100+CC if %TACTUAL% lss %TBASE% set /a TACTUAL=%TBASE%+%TACTUAL% if %TACTUAL% lss %TFIN% goto ESPERAR
On newer Windows OS versions you can use the command
in a DOS script (
.bat) to wait for 2s (2000 ms - substitute the time in ms you need). Be careful to include the
/w argument - without it the whole computer is put to sleep! You can use
-m instead of
/m if you wish and optionally a colon (:) between the w and the number.
You can make it with
This will be visible:
This will not be visible
timeout 5 >nul
I think the following command can help:
The syntax of the pause command is: pause d \\where d represents the duration in seconds
I am using Windows 7 (32 bit), but I don't know about the others.