Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 985
nonopolarity Avatar asked Nov 04 '09 08:11

nonopolarity


People also ask

How do you wait 1 second in a batch file?

Type in your command. TIMEOUT — Type timeout time where "time" is replaced by the number of seconds to delay. For example, typing in timeout 30 will delay your batch file for 30 seconds.

How do you repeat a command in cmd?

F3: Repeat the previous command. Up/Down Arrow: Scroll backward and forwards through previous commands you've typed in the current session.


2 Answers

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 echos:

@echo off echo %time% timeout 5 > NUL echo %time% 
like image 176
asveikau Avatar answered Sep 24 '22 17:09

asveikau


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 of ping, by redirecting it to nul.
like image 30
Martin Avatar answered Sep 24 '22 17:09

Martin