This topic will teach you one of the many useful things to know in the scripting language, batch file; Adding a delay/pause/timeout to your batch file.
The simplest way to make a delay or pause for a certain amount of time, is with the standard command
TIMEOUT. To make a timeout that lasts exactly one minute we type:
timeout /t 60
Now what is going on here?
First off we use the command
TIMEOUT with the parameter
/T (which simply means timeout) then we specify the amount of seconds to wait. In this case...
Timeout with the parameter /NOBREAK
If we take the example from before and run that in a BATCH file:
timeout /t 60 then while waiting those 60 seconds, you are actually able to break the timeout by pressing any key on your keyboard. To prevent this we simply add the parameter
/NOBREAK to the end of it.
timeout /t 60 /nobreak
By doing this it will timeout for 60 seconds, and if you want to break the timeout you will have to press (CTRL-C) on your keyboard.
When it's doing a timeout it will display:
Waiting for X seconds, press a key to continue ... or Waiting for X seconds, press CTRL+C to quit ... [This is with the /NOBREAK parameter]
To hide the message use the
NUL argument (For explanation of
NUL: Click Here)
timeout /t 60 > nul or timeout /t 60 /nobreak > nul
To make your script pause simply use the
This will display the text
Press any key to continue . . ., then add a newline on user input.
Let's say we want to create a "Hello World" program and after we click something on our keyboard, we want it to exit the program with the
echo Hello World pause exit
Here it uses the
ECHO command to say "Hello World". Then we use the
PAUSE command which displays
Press any key to continue . . . and then we use the
EXIT command to terminate the current BATCH script.
When it's pausing it will display:
Press any key to continue . . .
Hide the "Press any key to continue... prompt
To hide the message we redirect the output to a special device called
nul. This isn't actually a real device, but whatever we send to it is thrown away.
pause > nul
One of the most used command to delay for a certain amount of time is
PING -n 1 -w 1000 184.108.40.206 REM the -n 1 flag means to send 1 ping request. REM the -w 1000 means when the IP(220.127.116.11) does not respond, go to the next command REM 18.104.22.168 is an non-existing IP so the -w flag can ping a delay and go to next command
This would output the following on your batch file/console:
C:\Foo\Bar\Baz>ping -n -w 1000 22.214.171.124 Pinging 126.96.36.199 (Using 32 bytes of data) Request timed out Ping statistics for 188.8.131.52 Packets: Sent = 2,Received = 0, Lost = 1(100% loss)
Hide the text echoed out
>nul at the back of the command to redirect it to null.
ping -n w 1000 184.108.40.206 >nul
This would output nothing.
On older Windows system,
timeout is not available. However, we can use the
Very self-explanatory; sleep for 1 second.
sleep is a deperacted command and should be replaced by timeout.
This command is available on old Windows system. Also
SLEEP.exe is included in 2003 Resource Kit.
sleep.exe, put the executable file to
%Windir%\System32 folder. Then you can use it as normal command.