134

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

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

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Claudiu
  • 224,032
  • 165
  • 485
  • 680

12 Answers12

183

You can try

ping -n XXX 127.0.0.1 >nul

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tenfour
  • 36,141
  • 15
  • 83
  • 142
  • 3
    [Clay Calvert](http://groups.google.com/group/alt.msdos.batch.nt/msg/804a9fa0f5864558?pli=1) 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 '10 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 '11 at 09:55
  • 3
    Haha, so simple and yet so genius :-) Exactly what I was looking for. +1 – Simon Apr 13 '12 at 21:19
  • Only thing I could find that worked on plain XP. – Jesse Glick Mar 14 '13 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 '16 at 23:16
  • Yes, the SLEEP command doesn't seem to work for me too. Windows 10. Neat hack – peterb May 21 '17 at 02:48
  • For the `XXX=1` it exits immediately (Windows 7). Works only if `XXX>1`. – Andry Mar 13 '18 at 18:27
  • To wait in milliseconds may be this one fits better: `pathping localhost -n -q 1 -p XXX >nul`, where `XXX` is in milliseconds. Notice, if `localhost` is invalid ip address, then it exits immediately. – Andry Mar 13 '18 at 18:30
178

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

timeout <delay in seconds>
Jaime Soto
  • 3,168
  • 1
  • 21
  • 19
  • i have plain windows XP install. i think "sleep" is an addition you have to install. `timeout` works great, though, thanks! – Claudiu Nov 30 '10 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 '10 at 18:23
  • I haven't used this command - I just found it in [ss64](http://www.ss64.com). You may also want to take a look at [lukuluku's solution](http://stackoverflow.com/questions/4317020/windows-batch-sleep/4317114#4317114). – Jaime Soto Nov 30 '10 at 18:32
  • 1
    this works in Windows 7 – James Gardner May 20 '13 at 08:16
  • 2
    [The TIMEOUT command was introduced in Windows Vista](http://en.wikipedia.org/wiki/Batch_file#Sleep_or_scripted_delay). – Peter Mortensen Jul 24 '13 at 07:58
  • **N.B.** And if you don't want the message `Waiting for x seconds, press any key` to be displayed in the console, you can use `timeout delayInSecs >nul`, then it waits silently and continues when the time has elapsed. Bear in mind that it still reacts on any key (meaning someone pressing a key may interrupt the waiting of the script if the console in which the batch runs is the active window). – Matt Oct 09 '15 at 08:34
  • It's so weird -- I have windows 10, and it `sleep` works on my PC, but on a VM in Azure, which also has the *latest* Windows 10, it DOESN'T work. -- Luckily the ping trick above works fine. – BrainSlugs83 Jun 28 '18 at 21:23
  • Just one side note: timeout command lets you specify up to 99999 seconds (about 27 hours). If you need more wait time, you have to use the ping command, that can handle up to 2^32 seconds (ping -n XXX 127.0.0.1 >nul) – Niente0 Nov 09 '18 at 11:24
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

lukuluku
  • 4,344
  • 3
  • 30
  • 35
  • 2
    This is what I have been looking for and couldn't find anywhere. +1 – Registered User Dec 27 '12 at 06:04
  • 3
    The TIMEOUT command does not work on Windows XP; [it was introduced in Windows Vista.](http://en.wikipedia.org/wiki/Batch_file#Sleep_or_scripted_delay). – Peter Mortensen Jul 24 '13 at 08:10
  • 3
    On my XP machine here i have it. @PeterMortensen Did it got in via update? – Riscie Aug 21 '13 at 06:33
  • Absolutely no explanation? Please elaborate on your 3 parameters. `/t`, `/nobreak` and `> NUL`. – Heberda Dec 04 '15 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 '16 at 13:51
14

To wait 10 seconds:

choice /T 10 /C X /D X /N
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 1
    i'll use this with `> NUL` to suppress all output. – Claudiu Nov 30 '10 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 '10 at 18:55
  • 2
    The CHOICE command does not work on Windows XP; [it was introduced in Windows 2003 and Windows Vista.](http://ss64.com/nt/choice.html). – Peter Mortensen Jul 24 '13 at 08: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](http://www.computerhope.com/choicehl.htm) (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 '13 at 09: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.

emmax
  • 155
  • 1
  • 2
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PA.
  • 28,486
  • 9
  • 71
  • 95
  • 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](http://stackoverflow.com/questions/1107593/what-are-trade-offs-for-busy-wait-vs-sleep) 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 09 '15 at 09:11
  • This is unreliable on international Windows as it is tied to date and time localization. – risingballs Mar 13 '23 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • 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 '10 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 '10 at 18:52
  • The CHOICE command does not work on Windows XP; [it was introduced in Windows 2003 and Windows Vista.](http://ss64.com/nt/choice.html). – Peter Mortensen Jul 24 '13 at 08: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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wkl
  • 77,184
  • 16
  • 165
  • 176
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
3

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    /* Compile this with ms vc6, cl.exe Sleep.c, e.g. Sleep.exe 100 .. sleep for 100 ms */ #include int main(int argc, char *argv[]) { Sleep(argc<2?1000:atoi(argv[1])); return 0; } – mosh Sep 20 '16 at 06: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
Perception
  • 79,279
  • 19
  • 185
  • 195
Thomasso
  • 11
  • 1
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.

Rup
  • 33,765
  • 9
  • 83
  • 112