How do I make a batch file wait / sleep for some seconds?

58

18

I use a batch file to start up a few of the programs I need running in the background. Up until now, I had used the pause command to execute it after some of the other start-ups finished. I would prefer to use the wait or sleep commands but they do not appear to be included in Windows 7.

Anybody know how to put those commands back in, or a different method that achieves the same results?

Cegorach

Posted 2009-09-29T00:21:29.027

Reputation: 680

Wish a sleep.exe was provided by MS as default from 2017 in the system32 folder or in the program files/ any standard place. Simple c exe 40k or so! – tgkprog – 2015-11-03T09:19:05.797

Like this one http://www.sleepcmd.com/

– tgkprog – 2015-11-03T09:19:33.813

Windows batch: sleep – phuclv – 2018-06-20T04:40:07.003

Answers

30

There are many sleep utilities you can download and drop into your System32 folder, one is provided with the Windows Server 2003 Resource Kit called sleep.exe.

You can also use the ping trick:

:sleep
ping 127.0.0.1 -n 2 -w 1000 > NUL
ping 127.0.0.1 -n %1 -w 1000 > NUL

then from somewhere in your batch file, you can call it like so:

CALL :sleep 1

John T

Posted 2009-09-29T00:21:29.027

Reputation: 149 037

1the ping trick could be used as a temperary solution, but I would prefer one that just suspended the process, rather than have it do busy work for the time. Also, which one of the items in that list you linked was what I was looking for? – Cegorach – 2009-09-29T00:29:27.073

You want sleep.exe – John T – 2009-09-29T00:32:28.850

i tried CALL :sleep 1 from a test batch file and it responded with "system cannot find the batch label specified - SLEEP" – Cegorach – 2009-09-29T00:38:54.960

because you need to add the subroutine I posted above. Call just calls the subroutine, which is the 3 lines I posted above the call command. – John T – 2009-09-29T00:58:48.143

126

You can use the timeout command:

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.

For example, to wait for 10 seconds:

TIMEOUT /T 10

For more details:

TIMEOUT /?

Alex S

Posted 2009-09-29T00:21:29.027

Reputation: 1 269

1why isn't this chosen as the correct answer? the current one is far fetched if you ask me – Dany Khalife – 2012-08-04T18:50:27.367

7@Dany: because the topic is from 2009, when most installs were XP. Before Vista, Timeout was only in the server editions and in the Resource Kit, not on a normal XP install. Nowadays, indeed, the correct answer is Timeout. – mivk – 2012-10-31T18:47:29.910

6@DanyKhalife Unless you want to run the process in the background. "ERROR: Input redirection is not supported, exiting the process immediately." – AnnanFay – 2013-11-18T17:34:40.567

8

timeout /t <seconds> /nobreak > NUL

Prichard

Posted 2009-09-29T00:21:29.027

Reputation: 81

Works great! calc && timeout 3 && notepad – Tamara Wijsman – 2010-09-01T13:10:40.390

6

There is also

waitfor SomethingThatIsNeverHappening /t 10

Which will wait for between 1 and 99999 seconds.

Fowl

Posted 2009-09-29T00:21:29.027

Reputation: 651

1

sleep.exe is included in the Windows Server 2003 Resource Kit Tools.

You may use:

sleep /?
sleep seconds
sleep -m microseconds

harrymc

Posted 2009-09-29T00:21:29.027

Reputation: 306 093

-1

If you have Python installed (and added the install path to your environment variable), you can let Python do the sleeping with something like:

echo from time import sleep; sleep(3) | python

(If you have Windows Vista or higher, timeout naturally is the way to go, though.)

Protector one

Posted 2009-09-29T00:21:29.027

Reputation: 1 057