How do I get a Windows batch script to wait a few seconds?

sleep and wait don't seem to work (unrecognized command).

0

12 Answers

You can try

ping -n XXX 127.0.0.1 >nul 

where XXX is the number of seconds to wait, plus one.

8

I don't know why those commands are not working for you, but you can also try timeout

timeout <delay in seconds> 
8
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

8

To wait 10 seconds:

choice /T 10 /C X /D X /N 
4

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.

6

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.

1

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.

3

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.

1

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.

1

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.

5