Sleeping in a batch file

Posted on

Question :

Sleeping in a batch file

When writing a batch file to automate something on a Windows box, I’ve needed to pause its execution for several seconds (usually in a test/wait loop, waiting for a process to start). At the time, the best solution I could find uses ping (I kid you not) to achieve the desired effect. I’ve found a better write-up of it here, which describes a callable “wait.bat”, implemented as follows:

@ping -n 2 -w 1000 > nul
@ping -n %1% -w 1000> nul

You can then include calls to wait.bat in your own batch file, passing in the number of seconds to sleep.

Apparently the Windows 2003 Resource Kit provides a Unix-like sleep command (at last!). In the meantime, for those of us still using Windows XP, Windows 2000 or (sadly) Windows NT, is there a better way?

I modified the script in the accepted answer, so that it defaults to one second if no arguments are passed on the command line:

import time, sys

time.sleep(float(sys.argv[1]) if len(sys.argv) > 1 else 1)

Answer #1:


The timeout command, available from Windows Vista and onwards should be the command used, as described in another answer to this question. What follows here is an old answer.

Old answer

If you have Python installed, or don’t mind installing it (it has other uses too :), just create the following script and add it somewhere in your PATH:

import time, sys


It will allow sub-second pauses (for example, 1.5 sec, 0.1, etc.), should you have such a need. If you want to call it as sleep rather than, then you can add the .PY extension to your PATHEXT environment variable. On Windows XP, you can edit it in:

My Computer ? Properties (menu) ? Advanced (tab) ? Environment Variables (button) ? System variables (frame)

Answered By: tzot

Answer #2:

The timeout command is available from Windows Vista onwards:

c:> timeout /?

TIMEOUT [/T] timeout [/NOBREAK]

    This utility accepts a timeout parameter to wait for the specified
    time period (in seconds) or until any key is pressed. It also
    accepts a parameter to ignore the key press.

Parameter List:
    /T        timeout       Specifies the number of seconds to wait.
                            Valid range is -1 to 99999 seconds.

    /NOBREAK                Ignore key presses and wait specified time.

    /?                      Displays this help message.

NOTE: A timeout value of -1 means to wait indefinitely for a key press.

    TIMEOUT /?
    TIMEOUT /T 10
    TIMEOUT /T -1

Note: It does not work with input redirection – trivial example:

C:>echo 1 | timeout /t 1 /nobreak
ERROR: Input redirection is not supported, exiting the process immediately.
Answered By: Blorgbeard

Answer #3:

Using the ping method as outlined is how I do it when I can’t (or don’t want to) add more executables or install any other software.

You should be pinging something that isn’t there, and using the -w flag so that it fails after that amount of time, not pinging something that is there (like localhost) -n times. This allows you to handle time less than a second, and I think it’s slightly more accurate.


(test that isn’t taken)

ECHO Waiting 15 seconds

PING -n 1 -w 15000 > NUL
PING -n 15 -w 1000 127.1 >NUL
Answered By: daniel

Answer #4:

SLEEP.exe is included in most Resource Kits e.g. The Windows Server 2003 Resource Kit which can be installed on Windows XP too.

Usage:  sleep      time-to-sleep-in-seconds
        sleep [-m] time-to-sleep-in-milliseconds
        sleep [-c] commited-memory ratio (1%-100%)
Answered By: Luk

Answer #5:

I disagree with the answers I found here.

I use the following method entirely based on Windows XP capabilities to do a delay in a batch file:


REM DELAY seconds

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

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

You may also insert the day in the calculation so the method also works when the delay interval pass over midnight.

Answered By: Aacini

Answer #6:

I faced a similar problem, but I just knocked up a very short C++ console application to do the same thing. Just run MySleep.exe 1000 – perhaps easier than downloading/installing the whole resource kit.

Answered By: John Sibly

Answer #7:

Over at Server Fault, a similar question was asked, and the solution there was:

choice /d y /t 5 > nul
Answered By: mlsteeves

Answer #8:

You could use the Windows cscript WSH layer and this wait.js JavaScript file:

if (WScript.Arguments.Count() == 1)
    WScript.Echo("Usage: cscript wait.js seconds");
Answered By: Blake7

Leave a Reply

Your email address will not be published. Required fields are marked *