There are times in a batch file when it is required that it pause, either to give the user time to read whatever is on the screen or for the user to make some sort of input. It may be required that the program will wait for a number of seconds and continue if the user does not take some sort of other action.
Pause is probably the most common command used. It's the command that displays the familiar:
Press any key to continue . . .
Like many commands, the output of Pause can be suppressed by sending it to the null device. This can be useful if you want to add your own text as in this snippet:
echo Press any key to exit...
Choice is used to provide the user with a set of choices, he default is a simple yes(y) or no(n). It also has a timer switch. When this is used it waits for for a certain number of seconds then automatically uses a set answer. The user can press any of the preset keys to interrupt the timer. In standard use, the command will look like this:
Choice /t 10 /d y
This means give the user a Y/N prompt then wait for 10 seconds for a response. After 10 seconds use y (yes) as the response. The above command will generate its own prompt:
This can be suppressed using the >nul trick
Choice /t 10 /d y >nul
The /n switch can be used to suppress the prompt but the utility will display the user's response or the default choice when the timer expires. The list of choices can be altered using the /c switch and can be made case-sensitive using the /cs switch. These can be used to make guessing the choices more difficult but there is no guarantee that the user cannot interrupt the timer.
There are two methods of using Ping to produce a delay in batch files. One is to use it on an address that does not exist and the other is to use it on an address that does. There is some discussion about which is most accurate such as on Ron van der Woude's and StackOverflow's pages.
For an address that does exist, such as 127.0.0.1 use:
Ping -n 11 127.0.0.1 > nul
In the above case, for a delay of 10 seconds then 11 has to be specified in the command.
For an address that does not exist, such as 22.214.171.124 then use:
Ping 192.0.2.2 -n 1 -w 10000 > nul
In this case -n 1 means just one packet instead of 4 are sent and -w 10000 is the timeout in milliseconds.
What happens is that Ping waits one second for a reply, even if the reply is nearly instantaneous, before sending the next packets. This is how it can be used as a timer.
Sleep is not part of the normal instruction set put is part of the Server 2003 Resource Kit. Sleep takes a single argument, the time in seconds it is to wait for: Sleep cannot be interrupted by the user pressing a key.
Timeout can either be set to wait for a number of seconds or until a key is pressed or simply wait for the timer to run down and not respond to any key presses. by default, Timeout will display a countdown of how many seconds are left. This can, as with many other commands, be suppressed by sending the output to the null device by using >nul.
In the following examples the /t switch is used, but this is not necessary, just the time can be given.
The following will not display the countdown of 10 seconds but will allow the user to press any key to interrupt it:
Timeout /t 10 > nul
The following will not display the countdown of 10 seconds and will not allow the user to press any key to interrupt it:
Timeout /t 10 /nobreak > nul
Timeout can also be made to wait indefinitely for a key press by using a time of -1. This makes it act like the Pause command.
timeout /t -1 > nul
Interestingly, Timeout can be used with both the /t -1 and /nobreak switches. This means that the utility will wait indefinitely for a key press but will not accept any key presses! If this is used then Ctrl + C must be used to exit the utility.
using Waitfor this way is very uncommon, but it can be done. Waitfor sends or waits for a signal on a system and is used to synchronize computers across a network. The syntax to use it to delay a program for a period of time, in this case 10 seconds, is:
Waitfor something /t 10 >nul 2>&1
This will wait for 10 seconds before continuing execution and cannot be interrupted by the user.
The command needs an explanation:
Waitfor - the name of the command
something - the event to listen for in normal operation of Waitfor. Here we choose something that it cannot possibly send a signal
/t 10 - the switch that tells it to wait, here for 10 seconds
>nul 2>&1 - This is to suppress the error message that Waitfor is going to display when it didn't get the signal from "something." The error message will be "ERROR: timed out waiting for something." Normally >nul is enough to suppress messages from programs but Waitfor is sending an error message, not a normal program message.
stdin is 0, stdout is 1 and stderr is 2. >nul sends the stdout to the null device. 2>&1 sends stderr to stdout and we've just sent that to the null device.