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)
great, thanks, simple but not elegant, but works great!
ReplyDeleteOnce every 5 years or so Ive needed this kind of function but never looked too hard. Now finally after 15 years(!) - a solution!
ReplyDeleteNice and useful; Thanks.
ReplyDeleteHere'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
Hi
ReplyDeleteYou can use the following windows command...
TIMEOUT /T 30
This will pause for 30 seconds
You could always call a VBScript script to do a sleep:
ReplyDeletetest.bat :
start /wait cscript c:\sleep.vbs
then do something
c:\sleep.vbs :
Wscript.Sleep 10000 '10 Second wait
btw you can get a sleep version from gnuwin32 or unxutils project
ReplyDeleteD:\>sleep --version
GNU shellutils 1.9.4
Rather:
ReplyDeleteping 1.0.0.0 -n 1 -w 5000>NUL
So no results are seen :)
I think TIMEOUT /T n is exactly what you're looking for. Why Ping?
ReplyDelete@Anon:
ReplyDelete1) 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?
Err - Windows DOES have a sleep command. Its called (you'd never guess this) sleep.
ReplyDeleteC:\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>
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.
ReplyDeleteThanks 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
ReplyDeleteping 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
ReplyDeleteExample:
@echo off
echo Hello there!
ping 1.0.0.0 -n 1 -w 200>NUL
echo And this text comes 200 units later
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???
ReplyDeleteThis waits 10 seconds:
ReplyDeletechoice /T 10 /D Y > nul
@ECHO OFF
ReplyDeleteSETLOCAL 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