Windows batch: sleep

WindowsShellBatch FileScripting

Windows Problem Overview


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

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

Windows Solutions


Solution 1 - Windows

You can try

ping -n XXX 127.0.0.1 >nul

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

Solution 2 - Windows

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

timeout <delay in seconds>

Solution 3 - Windows

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

Solution 4 - Windows

To wait 10 seconds:

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

Solution 5 - Windows

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.

Solution 6 - Windows

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.

Solution 7 - Windows

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.

Solution 8 - Windows

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.

Solution 9 - Windows

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

Solution 10 - Windows

I just wrote my own sleep which called the Win32 Sleep API function.

Solution 11 - Windows

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.

Solution 12 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionClaudiuView Question on Stackoverflow
Solution 1 - WindowstenfourView Answer on Stackoverflow
Solution 2 - WindowsJaime SotoView Answer on Stackoverflow
Solution 3 - WindowslukulukuView Answer on Stackoverflow
Solution 4 - WindowsGolezTrolView Answer on Stackoverflow
Solution 5 - WindowsemmaxView Answer on Stackoverflow
Solution 6 - WindowsPA.View Answer on Stackoverflow
Solution 7 - WindowsClaudiuView Answer on Stackoverflow
Solution 8 - Windows逆さまView Answer on Stackoverflow
Solution 9 - WindowsAlessandro JacopsonView Answer on Stackoverflow
Solution 10 - WindowsDavid HeffernanView Answer on Stackoverflow
Solution 11 - WindowsRupView Answer on Stackoverflow
Solution 12 - WindowsThomassoView Answer on Stackoverflow