71

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?

Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
Cegorach
  • 810
  • 1
  • 7
  • 9
  • 1
    Or you could use powershell: http://stackoverflow.com/questions/1741490/how-to-tell-powershell-to-wait-for-each-command-to-end-before-starting-the-next – Berzemus Nov 09 '10 at 14:34
  • 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 Nov 03 '15 at 09:19
  • Like this one http://www.sleepcmd.com/ – tgkprog Nov 03 '15 at 09:19
  • [Windows batch: sleep](https://stackoverflow.com/q/4317020/995714) – phuclv Jun 20 '18 at 04:40

7 Answers7

148

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 /?
Gaff
  • 18,569
  • 15
  • 57
  • 68
Alex S
  • 1,489
  • 2
  • 9
  • 2
  • 1
    why isn't this chosen as the correct answer? the current one is far fetched if you ask me – Dany Khalife Aug 04 '12 at 18:50
  • 10
    @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 Oct 31 '12 at 18:47
  • 7
    @DanyKhalife Unless you want to run the process in the background. "ERROR: Input redirection is not supported, exiting the process immediately." – AnnanFay Nov 18 '13 at 17:34
33

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
  • 163,373
  • 27
  • 341
  • 348
  • 1
    the 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 Sep 29 '09 at 00:29
  • You want sleep.exe – John T Sep 29 '09 at 00:32
  • i tried CALL :sleep 1 from a test batch file and it responded with "system cannot find the batch label specified - SLEEP" – Cegorach Sep 29 '09 at 00:38
  • 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 Sep 29 '09 at 00:58
10
timeout /t <seconds> /nobreak > NUL
Prichard
  • 101
  • 1
  • 2
7

There is also

waitfor SomethingThatIsNeverHappening /t 10

Which will wait for between 1 and 99999 seconds.

Fowl
  • 671
  • 2
  • 7
  • 18
1

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

You may use:

sleep /?
sleep seconds
sleep -m microseconds

harrymc
  • 455,459
  • 31
  • 526
  • 924
-1

Here is a batch file macro that you can use:

@echo off & cls & setlocal disabledelayedexpansion
set LF=^


set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
set _delay=for /l %%a in (1 1 2) do if %%a==2 (%\n%
  for /f "tokens=1 delims=, " %%e in ("!argv!") do (%\n%
    set "_sec=%%~e" %\n%
    %__APPDIR__%ping.exe localhost -n !_sec! -w 1000 ^>nul 2^>^&1 %\n%
  ) %\n%
) else setlocal enabledelayedexpansion ^& set argv=,

You can call this in your batch file like:

%_delay% seconds

Like %_delay% 10 will wait for 10 seconds. To change the macro name replace "_delay" with anything else in fourth line.

Wasif
  • 7,984
  • 2
  • 19
  • 32
-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
  • 1,305
  • 3
  • 20
  • 29