Home » Questions » Computers [ Ask a new question ]

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

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

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.

Asked by: Guest | Views: 205
Total answers/comments: 4
bert [Entry]

"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"
bert [Entry]

timeout /t <seconds> /nobreak > NUL
bert [Entry]

"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."
"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."
bert [Entry]

"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.)"