Thursday, November 08, 2007

wait or pause in windows batch script

problem:
i am told that windows .bat files do not have a sleep or wait command.
this is pretty sucky for when you want to wait or sleep, isnt it?

solution:
do this terrible hack
ping 1.0.0.0 -n 1 -w 5000 >NUL

that will block for 5 seconds. yey!

also, if you need to wait for a process to finish, i blogged about that here

also, if you have the ability to install something on the box where you will run your script, you should get the Windows Server 2003 Resource Kit tools from Microsoft: which works with XP. This gives you a real sleep command! (Thanks to Tricky for pointing this out in the comments)

16 comments:

  1. great, thanks, simple but not elegant, but works great!

    ReplyDelete
  2. Once every 5 years or so Ive needed this kind of function but never looked too hard. Now finally after 15 years(!) - a solution!

    ReplyDelete
  3. Nice and useful; Thanks.
    Here's a small improvement:
    ping 1.0.0.0 -n 1 -w 5000 >NUL
    Now you don't see the distracting output of the ping command.
    -Neil

    ReplyDelete
  4. Hi

    You can use the following windows command...

    TIMEOUT /T 30

    This will pause for 30 seconds

    ReplyDelete
  5. You could always call a VBScript script to do a sleep:

    test.bat :

    start /wait cscript c:\sleep.vbs

    then do something


    c:\sleep.vbs :

    Wscript.Sleep 10000 '10 Second wait

    ReplyDelete
  6. btw you can get a sleep version from gnuwin32 or unxutils project

    D:\>sleep --version
    GNU shellutils 1.9.4

    ReplyDelete
  7. Rather:
    ping 1.0.0.0 -n 1 -w 5000>NUL

    So no results are seen :)

    ReplyDelete
  8. I think TIMEOUT /T n is exactly what you're looking for. Why Ping?

    ReplyDelete
  9. @Anon:
    1) i am a newb at making bat files, so i use the most primitive method that gets the job done
    2) i dont have a timeout function in windows xp. what am i missing?

    ReplyDelete
  10. Err - Windows DOES have a sleep command. Its called (you'd never guess this) sleep.

    C:\WINDOWS>sleep
    Usage: sleep time-to-sleep-in-seconds
    sleep [-m] time-to-sleep-in-milliseconds
    sleep [-c] commited-memory ratio (1%-100%)

    C:\WINDOWS>sleep 10

    C:\WINDOWS>

    ReplyDelete
  11. Re previous post. It seems that the sleep command is not available by default - it is included with the Windows Server 2003 Resource Kit tools from Microsoft: http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en which works with XP.

    ReplyDelete
  12. Thanks for the info, that sleep would be useful for 50% of the time when you are running on a box where you can install that toolkit. ah, microsoft :P

    ReplyDelete
  13. ping 1.0.0.0 -n 1 -w 200>NUL is a very good sleep, wait etc... you don't have to download anything like windows server 2003 res. tool kit :) This works on xp

    Example:

    @echo off
    echo Hello there!
    ping 1.0.0.0 -n 1 -w 200>NUL
    echo And this text comes 200 units later

    ReplyDelete
  14. Interesting post as I was not sure there was a sleep command. More interesting is that it seems to have been removed from Windows 7???

    ReplyDelete
  15. This waits 10 seconds:
    choice /T 10 /D Y > nul

    ReplyDelete
  16. @ECHO OFF

    SETLOCAL enabledelayedexpansion
    SET cncvnx_spa=10.1.x
    SET cncvnx_spb=10.1.6x
    SET grfVnx_spa=10.1.9x
    SET grfvnx_spb=10.1.9x



    :Home
    :PING

    FOR %%A IN (cncvnx_spa,cncvnx_spb,grfvnx_spa,grfvnx_spb) DO (
    PING -n 1 -l 5 "%%A" >NUL
    IF !errorlevel! == 0 (
    ECHO %date% %time% IS OK : [%%A]>>%%A.log
    ) ELSE (
    ECHO %date% %time% ERROR : [%%A]
    ECHO %date% %time% ERROR : [%%A]>>%%A.log
    )
    )
    ECHO.


    REM WILL TIME DELAY FOR 1 SECONDS
    CHOICE /D Y /T 1 >NUL
    GOTO PING

    i m using the above scripts but it is keep on running. i just need the 4 lines output for ping like we get with ping ip command

    ReplyDelete