How do I get a Windows batch script to wait a few seconds?
sleep
and wait
don't seem to work (unrecognized command).
You can try
ping -n XXX 127.0.0.1 >nul
where XXX is the number of seconds to wait, plus one.
-n
is used to indicate the number of requests. ping
waits one second by default for each reply even if it arrives in less time.
Nov 30, 2010 at 18:27
hardware error
message, and ran through the supposedly 1-second long pings in a fraction of the expected time.
Jan 28, 2016 at 23:16
I don't know why those commands are not working for you, but you can also try timeout
timeout <delay in seconds>
timeout
works great, though, thanks!
timeout /t 10 /nobreak > NUL
/t
specifies the time to wait in seconds
/nobreak
won't interrupt the timeout if you press a key (except CTRL-C)
> NUL
will suppress the output of the command
/t
, /nobreak
and > NUL
.
/t
Time to wait in seconds, /nobreak
won't interrupt the timeout if you press a button (except CTRL-C
), > NUL
will suppress the output of the command
To wait 10 seconds:
choice /T 10 /C X /D X /N
Choice
is available for Windows XP but it is not installed by default. It wasn't introduced in Windows 2003, though, because it existed in earlier consumer versions (95, 98) of Windows and even in MSDOS 6.0. They probably just forgot about it when they combined the consumer versions with the NT versions starting with Windows 2000. ;-)
Jul 24, 2013 at 9:13
Microsoft has a sleep function you can call directly.
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)
You can just say sleep 1 for example to sleep for 1 second in your batch script.
IMO Ping is a bit of a hack for this use case.
For a pure cmd.exe script, you can use this piece of code that returns the current time in hundreths of seconds.
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
You may then use it in a wait loop like this.
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
And putting all pieces together:
@echo off
setlocal enableextensions enabledelayedexpansion
call :gettime t1
echo %t1%
call :wait %1
call :gettime t2
echo %t2%
set /A tt = (t2-t1)/100
echo %tt%
goto :eof
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
For a more detailed description of the commands used here, check HELP SET
and HELP CALL
information.
timeout x >nul
after the :wait
label (replace x by the number of seconds to wait)
Heh, Windows is uhm... interesting. This works:
choice /T 1 /d y > NUL
choice
presents a prompt asking you yes or no. /d y
makes it choose yes. /t 1
makes it wait a second before typing it. > NUL
squashes output.
The Windows 2003 Resource Kit has a sleep
batch file. If you ever move up to PowerShell, you can use:
Start-Sleep -s <time to sleep>
Or something like that.
powershell -command "Start-Sleep -Milliseconds 5000">nul
Jun 21, 2016 at 17:11
I rely on JScript. I have a JScript file like this:
// This is sleep.js
WScript.Sleep( WScript.Arguments( 0 ) );
And inside a batch file I run it with CScript (usually it is %SystemRoot%\system32\cscript.exe
)
rem This is the calling inside a BAT file to wait for 5 seconds
cscript /nologo sleep.js 5000
I just wrote my own sleep which called the Win32 Sleep API function.
RJLsoftware has a small utility called DelayExec.exe. With this you can execute a delayed start of any program in batches and Windows registry (most useful in ...Windows/.../Run registry).
Usage example:
delayexec "C:\WINDOWS\system32\notepad.exe" 10
or as a sleep command:
delayexec "nothing" 10
Personally I use a Perl one-liner:
perl -e "sleep 10;"
for a 10-second wait. Chances are you'll already have Perl installed on a development machine as part of your git installation; if not you will have to install it, for example, from ActiveState or Strawberry, but it's one of those things I install anyway.
Alternatively, you can install a sleep command from GnuWin32.
ruby -e "sleep 10"
-- and it's one character less :)
Oct 18, 2011 at 19:19
perl -e "sleep 9"
:-)