I have a batch file:
arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx ipconfig /flushdns
How can I do these two commands on Windows XP, every 10 seconds?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
Try this one:
:loop arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx ipconfig /flushdns ping localhost -n 11 > nul goto loop
The ping command will execute for 10 seconds, and all the output will be redirected to the NUL device, meaning that you will see no output from the ping command. It works indeed as a "sleep" command would.
:top arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx ipconfig /flushdns sleep 10 goto top
Edit: As mentioned in the comments,
SLEEP won't be available in a normal install. You'll need something like the Windows 2003 Resource Kit, or some other trick to simulate it (the ping trick Bruno mentions), and notes to do so can be found here.
Install Cygwin which will make
cron available to you (among other things).
All of the previous answers are flawed: Instead of making each loop take 10 seconds, they cause each loop to take 10 seconds plus the execution time of the desired workload. In a correct solution, each loop must calculate the number of seconds to pause after it's finished executing the loop's workload, by subtracting the current time from the Desired Loop Endtime. At the beginning of each loop, the desired loop duration (10 seconds or whatever) should be added to the previous Desired Loop Endtime. (During initialization before the first loop begins, set Desired Loop Endtime to the current time.) This scheme ensures that for all N, the total time to execute N loops will be within one second of the desired total amount of time (N x DesiredLoopDuration).
The TIMEOUT command can be used for the pauses to minimize cpu overhead.
Complications that need to be tested each loop are midnight rollover (in which case 86400 seconds must be subtracted from Desired Loop Endtime) and changes to or from Daylight Savings Time (in which case 3600 seconds must be added to or subtracted from Desired Loop Endtime).
The checks for midnight rollover and Daylight Savings change are easy to implement if it can be assumed that the loop duration is less than an hour. A general solution that will allow a desired loop duration greater than an hour can simply split the desired duration over multiple inner loops, each less than an hour. (Execute the workload once in each outer loop, and use the inner loops only for pausing the correct number of seconds and the checks.) Where I wrote above that the desired loop duration should be added to Endtime at the start of each loop, in the general solution it means the desired inner loop duration should be added to Endtime at the start of each inner loop.
Here's my .bat script:
@echo off set /A "DesiredSeconds=%1" setlocal EnableDelayedExpansion rem Since DesiredSeconds might be very long, split the loop into rem pieces each less than an hour to simplify Daylight Savings tests. set /A "MaxSecondsPerLoop=1800" rem For debugging, use a small maxloop duration: rem set /A "MaxSecondsPerLoop=35" rem There will be zero or more inner loops of maxsecondsperloop duration, rem plus a final inner loop that completes the outer loop duration. set /A "NumberOfLoops=1+(DesiredSeconds/MaxSecondsPerLoop)" set /A "FinalLoopSeconds=(DesiredSeconds%%MaxSecondsPerLoop)" rem Initialize Endtime to current time, in seconds after midnight. rem Note that HH may have a leading blank space, while MM and SS may have rem a leading zero (octal confusion). If HH can have a leading zero rem in your locale, you'll need to modify this code accordingly, rem and the code in the inner loop below too. for /F "tokens=1-3 delims=:." %%a in ("%time%") do ( set /A "EndTime=(3600*%%a)+(60*(1%%b-100))+(1%%c-100)" ) :loop call :workload %2 %3 %4 %5 %6 %7 %8 %9 rem Now wait long enough so the total loop duration will be as desired. for /L %%G in (1,1,%NumberOfLoops%) do ( rem Set Endtime to the desired end of the inner loop: if %%G LSS !NumberOfLoops! ( set /A "EndTime+=MaxSecondsPerLoop" ) else ( set /A "EndTime+=FinalLoopSeconds" ) rem To calculate the number of seconds to pause, and to check for rem midnight rollover and change to/from Daylight Savings Time, rem we need to know the current time, as seconds after midnight. for /F "tokens=1-3 delims=:." %%a in ("!time!") do ( set /A "CurrentTime=(3600*%%a)+(60*(1%%b-100))+(1%%c-100)" ) rem We passed midnight if endtime is MUCH greater than currenttime rem so in that case subtract 24 hours from endtime. set /A "TimePlus12Hrs=CurrentTime+43200" if !EndTime! GTR !TimePlus12Hrs! set /A "EndTime-=86400" rem A change to Daylight Savings Time occurred if endtime < currenttime rem so in that case add an hour to endtime. if !EndTime! LSS !CurrentTime! set /A "EndTime+=3600" rem A change to Standard Time occurred if endtime>currenttime+3600 rem so in that case subtract an hour from endtime. set /A "TimePlus1Hr=CurrentTime+3600" if !EndTime! GTR !TimePlus1Hr! set /A "EndTime-=3600" set /A "SecsToWait=EndTime-CurrentTime" echo %time% Pausing !SecsToWait! seconds to complete the loop... TIMEOUT /t !SecsToWait! /NOBREAK >nul ) goto :loop :workload rem For testing, simulate a workload that lasts for rem a variable length of time from 0 to 9 seconds: for /F "tokens=1-4 delims=:." %%a in ("%time%") do ( set /A "WorkSecs=(1%%d-100)%%10" ) echo %time% Simulating workload for approximately !WorkSecs! seconds... TIMEOUT /t !WorkSecs! /NOBREAK >nul exit /B endlocal
below, it is both an ugly and a beautiful way, an Windows batch file - it consumes a lot of cpu time to do nothing, but it does what you want and is so nice.
SETLOCAL EnableDelayedExpansion ::in seconds set time2stop=10 :loop1 arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx ipconfig /flushdns for /f "tokens=1,2* delims=:" %%i in ("!time!") do ( set hour1=%%i set min1=%%j set sec1=%%k ) for /f "tokens=1* delims=," %%i in ("!sec1!") do ( set isec1=%%i ) :loop2 for /f "tokens=1,2* delims=:" %%i in ("!time!") do ( set hour2=%%i set min2=%%j set sec2=%%k ) for /f "tokens=1* delims=," %%i in ("!sec2!") do ( set isec2=%%i ) set /a delta=3600*(!hour2!-!hour1!)+60*(!min2!-!min1!)+(!isec2!-!isec1!) if !delta! geq !time2stop! goto end2 goto loop2 :end2 @echo on goto loop1