38

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?

quack quixote
  • 42,640

9 Answers9

59

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
Tony
  • 691
  • 1
    The right answer for any version of Windows made in the last decade (including Windows XP specified in the question). Thanks :-) – woot Apr 09 '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 '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 https://superuser.com/questions/66975/do-something-every-10-seconds-batch-file/276739 – user2987828 Mar 21 '21 at 16:31
19

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.

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.

  • 6
    sleep isn't included by default. You need the resource kit or something to get this? – marcc Nov 07 '09 at 00:19
  • What exactly do I need? –  Nov 07 '09 at 00: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. –  Nov 07 '09 at 00: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. –  Nov 07 '09 at 00:29
2

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.

Hayz
  • 21
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

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
  • 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
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à!

Dan McGrath
  • 2,996
  • 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. –  Nov 07 '09 at 00:22
  • I didn't think choice came with XP and later, or at least I haven't seen it on any recent installs. –  Nov 07 '09 at 00:29
  • 1
    I'm running vanilla Vista Home Premium on this laptop and it works fine. –  Nov 07 '09 at 00: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).

mob
  • 262
-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
studiohack
  • 13,490
kokbira
  • 5,377
  • 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
  • All you say I foresaw. But it is AN way when you have only Windows to do that. The midnight bug would be corrected simply doing a "if" (doing some modificatioks like 'if "!hour1! equ "23" if "!min1!" equ "59" if !isec1! geq 50 set flag=1' and 'if "!flag!" equ "1" ( set hour1=0 set hour2=0 )'. And wasting CPU time will be a great problem in that case only if you are repeating it for a long time or if you are doing a "critical task". – kokbira Apr 27 '11 at 17:31
  • Well, it also will prevent the CPU from entering a lower power state which will do awful things to your laptop battery life. You won't want to run this for multiple users on a terminal server as well ... hogging the CPU just for the sake of waiting is never a good idea, really. Besides, choice, ping and timeout are standard Windows commands that will work on nearly every Windows machine out there (well, ping at least; choice wasn't added until XP, I think and timeout might also not be available on legacy versions). – Joey Apr 27 '11 at 18:07
  • "choice" is not a choice for Windows after Win98 (there is no "choice" command). "Ping" generates "parasite traffic", and other superuser comrades already covered that solution. I don't know "timeout" command. I tried to show a different solution and I also warned about the problems using it. Why don't you contribute with your "timeout" solution? :) – kokbira Apr 27 '11 at 19:31
  • choice does exist on my Windows 7 machine. ping with localhost will never rach anywhere outside the local machine. – Joey Apr 27 '11 at 20:34
  • you are correct about commands "choice" and "timeout" but you have to pay attention: he says "windows xp". so i cannot post these solutions - or do you know an way to use them on xp? – kokbira Apr 27 '11 at 22:51