I think this is a question about variable scope. I am trying to understand why the %average% variable can contain anything other than "0" when the line
echo average before %average% is reached.
It appears to contain the value from the previous run through the loop. It also does not get updated by the following
set /a and so stays the same in
echo average after %average%. The next run around the loop it has the correct value from the previous run - but not this time around.
@echo off setlocal set count=1 set four=0 set ping1=0 set ping2=0 set ping3=0 set ping4=0 set average=0 :loop set pingtime= set striptime= @for /f "tokens=3 delims==" %%G in ('ping -n 1 220.127.116.11 ^| find "Reply from"') do set pingtime=%%G rem set pingtime=time=100ms echo "ping %pingtime%" if "%pingtime%"=="" ( set striptime=6666 ) else ( rem set striptime=%pingtime:~5,6% rem echo "Strip1 %striptime%" set striptime=%pingtime:ms TTL=% echo Strip2=%striptime% ) set ping%count%=%striptime% @echo ping%count% time=%striptime% echo %ping1% %ping2% %ping3% %ping4% set /a count+=1 if %count%==5 ( set count=1 set four=1 ) rem echo "Four= %four%" if %four%==1 ( set average=0 echo average before=%average% echo %ping1% %ping2% %ping3% %ping4% set /a average=%ping1%+%ping2%+%ping3%+%ping4% echo average after=%average% if %average% GTR 2500 ( set four=0 set ping1=0 set ping2=0 set ping3=0 set ping4=0 rasdial nextgdu /disconnect rasdial nextgdu rem | choice /c:ab /t:A,2 > NUL ) ) goto loop