34

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?

2
  • 3
    You can use Windows Scheduled Tasks to get it down to once a minute. – Jay Riggs Nov 7 '09 at 0:18
  • it is possible with Windows batch: see my solution below. the only problem is cpu usage... – kokbira Apr 25 '11 at 20:05
46

this makes a 10 sec. delay:

timeout /t 10

so try this:

 :loop
 arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
 ipconfig /flushdns
 timeout /t 10
 goto loop
4
  • 1
    The right answer for any version of Windows made in the last decade (including Windows XP specified in the question). Thanks :-) – woot Apr 9 '16 at 17:53
  • 2
    It actually waits -1 - 99 999 seconds (~~1day3,7h|windows7) - the number specified in /t OR for pressing any key - that can be turned off by sending /NOBREAK param :) – jave.web Oct 30 '16 at 14:42
  • @Tony if you add a comment about timeout being interruptible by a keypress, you will prove that you do not yet have become a ghost as of 2021 :-) – user2987828 Mar 21 at 15:14
  • @Tony this solution updates the display every second, for hour-long waits (windows 10 home does not have sleep) I prefer the solution superuser.com/questions/66975/… – user2987828 Mar 21 at 16:31
18

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.

0
11
: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.

4
  • 6
    sleep isn't included by default. You need the resource kit or something to get this? – marcc Nov 7 '09 at 0:19
  • What exactly do I need? – steven Nov 7 '09 at 0:21
  • It isn't? I could've sworn it was something I could count on, but I guess like choice it might not be included by standard. I'll look into it more. – Doug Kavendek Nov 7 '09 at 0:21
  • 1
    There's no need to install anything else on windows to "fake" a sleep command. Take a look at my answer for an example. – Bruno Reis Nov 7 '09 at 0:29
2

More precise solution ping unexistent host once and set timeout

ping 1.0.0.0 -n 1 -w 10000 >nul

But it generate parasite traffic

1
  • 1
    1.0.0.0 does now exist. I use www.ens.fr that won't answer pings, but a better suggestion would be nice. – user2987828 Mar 21 at 16:25
1

You can use the for and ping command:

@echo off
for /l %%a in (0,0,0) do (
arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
ipconfig /flushdns
ping -n 11 localhost >nul
)

You can use ping -n [secs+1] localhost >nul to wait a certain amount of time in seconds.

2
  • 1
    Why is this better than exactly the same ping command suggested in another answer? – RalfFriedl Feb 21 '19 at 21:44
  • 1
    To be fair, this is the first answer that shows how to do an infinite loop without using goto. – Scott Feb 21 '19 at 22:08
0

Cheat:

Use this command to pause the batch for 10 seconds

choice /n/t:c,<10>/c:cc

Now, place it in a never ending loop in the batch and voilà!

4
  • 1
    Or, if you want, download the resource kit or write a simple prog to sleep 10. My method however, will mean you can move it to another machine without having to download more software again. – Dan McG Nov 7 '09 at 0:22
  • I didn't think choice came with XP and later, or at least I haven't seen it on any recent installs. – Doug Kavendek Nov 7 '09 at 0:29
  • 1
    I'm running vanilla Vista Home Premium on this laptop and it works fine. – Dan McG Nov 7 '09 at 0:35
  • there is no "choice" for windows xp!!! – kokbira Apr 25 '11 at 19:27
0

Install Cygwin which will make sleep and cron available to you (among other things).

1
  • 2
    Overkill for what he is asking! – Bruno Reis Nov 7 '09 at 0:31
0

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
1
  • Another reason to prefer this solution -- which recalculates the number of seconds to pause in order to maintain the desired timeline -- is that other processes running on Windows can at times interfere with the execution of the .bat file, causing the pause command or the workload to take many more seconds than expected. Two of my "precisely timed" .bat apps experienced such interference this morning, perhaps as a result of Macrium Reflect beginning its morning differential backup. The next pause was automatically shortened to restore the desired timeline. – Dolores Stevens Mar 21 '20 at 10:52
-1

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
10
  • another way is to do some loop sequence, for example repeating ( for /l %%i in (1,1,65535) do set nothing=0 ) some times and "calibrating" "how many times" you have to repeat testing to result in 10 seconds, hahaha. but it is the ugliest way. – kokbira Apr 25 '11 at 20:10
  • you can also create some Windows batch to use as functions (one to split time, one to do the waiting...) but it will be more complex... – kokbira Apr 25 '11 at 20:13
  • I think it will cause a bug if you are using it from 23:59:51 to 23:59:59 - in that case, the batch will wait forever... some "if's" would solve the problem... – kokbira Apr 25 '11 at 20:17
  • if you want to test for a fixed time (not a time+10sec) you can simply do "if %time% geq 10:30 do goto end". the solution became complex because the sum... – kokbira Apr 25 '11 at 20:21
  • It's actually horrible. First of all, busy waiting. wasting CPU cycles unnecessarily is never a good idea (unless on embedded hardware). This also doesn't appear to work correctly around midnight. – Joey Apr 27 '11 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy