Delay a Batch File

Need to delay an action in your batch file? You can set your batch file to wait until the user signals he or she is ready to continue, or you can set your batch file to wait for a set amount of time before proceeding automatically. The method you choose depends on the needs of your program as well as your system’s capabilities.

Steps

PAUSE

  1. Use the PAUSE command to make the program wait for the user. This will cause the computer to wait for any key to be pressed. The key will not be printed to the screen. If you want to delay for a specific amount of time, see one of the following methods. [1]
  2. Place the PAUSE command where you want the program to wait. The user will be prompted to continue.
  3. Hide the PAUSE dialog. You can add >nul to the end of any command to send its output to the "Null Device" so it doesn't appear on the screen. Make sure you don't add >null to the end; that will create a new file called null and put the output there.

TIMEOUT

  1. Use the TIMEOUT command to make the program wait a specified amount of time. The user can skip this waiting period, or you can apply a parameter that keeps the user from skipping.
    • TIMEOUT may not work with older systems. Try the deprecated SLEEP command if the TIMEOUT command isn't working.
  2. Place the TIMEOUT command where you want the program to wait. Add the flag /t followed by the number of seconds.
  3. Keep the user from skipping the break. Add the flag /nobreak to keep any input from skipping the delay.
    • The user can still end the program by pressing Ctrl + C (keyboard interrupt).

SLEEP

  1. Use the SLEEP command to make the program wait the specified number of seconds. SLEEP is deprecated; if possible TIMEOUT should be used instead.
  2. Place the SLEEP command where you want to program to wait. Add the number of seconds you want it to wait after the SLEEP command. The SLEEP command cannot be interrupted by the user.

PING

  1. Use the PING command to force the program to wait by sending a message to another computer and waiting for a reply. You must ping a non-valid IP address, otherwise a computer might actually reply, and the delay won't be long enough. You cannot ping a non-valid hostname, such as madeup.example.com otherwise error messages will be shown instead of a delay.
  2. Insert the PING command where you want the delay to occur. The -n 1 parameter adds a second ping to the original command. The wait between the two pings, designated by -w 30000, is the delay. The delay has to be entered as milliseconds.[2] Putting >nul at the end will send the command's output to the "Null Device".

Sources and Citations

Related Articles