134

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 12

183

You can try

ping -n XXX 127.0.0.1 >nul

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

8
  • 3
    Clay Calvert provides an explanation of this technique. Note that -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.
    – Jaime Soto
    Nov 30, 2010 at 18:27
  • remember you need to add one to the number of seconds, because ping doesn't wait before the first request.
    – lunixbochs
    Mar 21, 2011 at 9:55
  • 3
    Haha, so simple and yet so genius :-) Exactly what I was looking for. +1
    – Simon
    Apr 13, 2012 at 21:19
  • Only thing I could find that worked on plain XP. Mar 14, 2013 at 16:19
  • I ran into a funny issue using this method when I needed a script to automatically release the computer's IP address, wait a few minutes while I reconfigured a switch port, and then renew the IP address. During the intervening moments that the switch port was down, the network connection was lost and ping errored out with a hardware error message, and ran through the supposedly 1-second long pings in a fraction of the expected time.
    – Wes Larson
    Jan 28, 2016 at 23:16
178

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

timeout <delay in seconds>
8
  • i have plain windows XP install. i think "sleep" is an addition you have to install. timeout works great, though, thanks!
    – Claudiu
    Nov 30, 2010 at 18:21
  • Ooh! I didn't know about timeout. Unfortunately it isn't available in Windows 2000, although that probably isn't a problem nowadays. If it is, choice will work on previous versions too (even in DOS).
    – GolezTrol
    Nov 30, 2010 at 18:23
  • I haven't used this command - I just found it in ss64. You may also want to take a look at lukuluku's solution.
    – Jaime Soto
    Nov 30, 2010 at 18:32
  • 1
    this works in Windows 7 May 20, 2013 at 8:16
  • 2
170
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

5
  • 2
    This is what I have been looking for and couldn't find anywhere. +1 Dec 27, 2012 at 6:04
  • 3
    The TIMEOUT command does not work on Windows XP; it was introduced in Windows Vista.. Jul 24, 2013 at 8:10
  • 3
    On my XP machine here i have it. @PeterMortensen Did it got in via update?
    – Riscie
    Aug 21, 2013 at 6:33
  • Absolutely no explanation? Please elaborate on your 3 parameters. /t, /nobreak and > NUL.
    – Heberda
    Dec 4, 2015 at 14:41
  • 2
    @Heberda /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
    – lukuluku
    Jan 11, 2016 at 13:51
14

To wait 10 seconds:

choice /T 10 /C X /D X /N
4
  • 1
    i'll use this with > NUL to suppress all output.
    – Claudiu
    Nov 30, 2010 at 18:52
  • woah actually i have neither timeout nor choice on this install of xp i'm working with... really weird. ping it is.
    – Claudiu
    Nov 30, 2010 at 18:55
  • 2
    The CHOICE command does not work on Windows XP; it was introduced in Windows 2003 and Windows Vista.. Jul 24, 2013 at 8:01
  • 2
    @PeterMortensen Turns out 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. ;-)
    – GolezTrol
    Jul 24, 2013 at 9:13
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.

6
  • 10
    "C:\Documents and Settings\User>sleep 'sleep' is not recognized as an internal or external command, operable program or batch file." Windows is a bit of a hack for this use case.. lol
    – Claudiu
    Aug 4, 2011 at 15:37
  • I could use 'sleep' on my machine (windows 7). Jun 30, 2012 at 16:52
  • 4
    PowerShell or plain cmd.exe? Dec 18, 2012 at 17:52
  • 1
    PowerShell indeed. Good catch @RandomInsano!
    – Domi
    Dec 9, 2014 at 7:28
  • 2
    Confirmed - it is a POWER SHELL command, not regular command line (cmd.exe)
    – jave.web
    May 3, 2015 at 21:14
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.

2
  • 6
    But that is a "busy wait" loop burning CPU time unnecessarily. Calling a command which waits is better in my opinion. Please consider this link at SO to find out more. In order to mitigate this in your script you could insert timeout x >nul after the :wait label (replace x by the number of seconds to wait)
    – Matt
    Oct 9, 2015 at 9:11
  • This is unreliable on international Windows as it is tied to date and time localization. Mar 13 at 22:32
4

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
  • 8
    Yeah, but you need to have a Windows that asks Yes or No. On my Dutch Windows, it asks Ja or Nee, so the Y won't work. Thats why I specified the exact choices in my answer above. And you can just add /N to prevent the prompt to be displayed.
    – GolezTrol
    Nov 30, 2010 at 18:38
  • @GolezTrol: still need the "> NUL" to supress the "X" from displaying. ah and I didn't realize the importance of "/C" , the example i found online had it
    – Claudiu
    Nov 30, 2010 at 18:52
  • The CHOICE command does not work on Windows XP; it was introduced in Windows 2003 and Windows Vista.. Jul 24, 2013 at 8:02
4

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
  • ...or powershell -command "Start-Sleep -Milliseconds 5000">nul Jun 21, 2016 at 17:11
4

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
3

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

1
  • 1
    /* Compile this with ms vc6, cl.exe Sleep.c, e.g. Sleep.exe 100 .. sleep for 100 ms */ #include <WinSock.h> int main(int argc, char *argv[]) { Sleep(argc<2?1000:atoi(argv[1])); return 0; }
    – mosh
    Sep 20, 2016 at 6:57
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
1

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
  • ah it isn't one of those things I install =P. i don't even want to install a "sleep.exe" utility, so this is a bit much.
    – Claudiu
    Nov 30, 2010 at 18:19
  • 1
    Ruby would work too: ruby -e "sleep 10" -- and it's one character less :)
    – Roy Tinker
    Oct 18, 2011 at 19:19
  • 1
    @Roy: Actually, perl can do without the semicolon. And if you can get away with 9 seconds instead of 10, it is even TWO characters less: perl -e "sleep 9" :-)
    – mivk
    Oct 31, 2012 at 18:37
  • 1
    You can also use PHP like this: php -r sleep(5);
    – jmp
    Jan 15, 2016 at 17:33
  • 1
    Here's another interpreter: node -e "setTimeout(a=>{}, 500)" Jan 19, 2020 at 19:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.