1

I have a first loop and a second loop but the variable from the first loop always stays the same in the second loop. how do i get the actual value of the second loop?

REM @echo off
set fastestserver=none
set fastestresponse=99999999ms

for /F "tokens=*" %%A in (fileservers.txt) do (
        for /F "skip=8 tokens=9 delims= " %%B in ('ping -n 3 %%A') do (
        echo %%A
        echo %%B
        set fastestresponse=%%B
        set actualpingserver=%%B
            if /I "%fastestresponse:~,-2%" GTR "%actualpingserver:~,-2%" (
            set fastestresponse=%%B
            set fastestserver=%%A
        )
    )
)

REM @echo on
echo %fastestserver%
echo %fastestresponse%

in the fileserver.txt there are some servers inside, each get pinged and i want to get the average ping, if the ping is smaller then of the one before it should replace the 2 variables (fastestserver , fastestresponse ).

Problem now is that if i debug the script it always takes for

if /I "%fastestresponse:~,-2%" LSS "%actualpingserver:~,-2%"

so somehow from the second for the fastestresponse does not get filld because the value is always 999999999ms.

Thanks already for a hint/helping answer

1
  • you need delayed expansion Commented Sep 3, 2015 at 8:18

1 Answer 1

3

try this:

REM @echo off

setlocal enableDelayedExpansion
set fastestserver=none
set fastestresponse=99999999ms

for /F "tokens=*" %%A in (fileservers.txt) do (
        for /F "skip=8 tokens=9 delims= " %%B in ('ping -n 3 %%A') do (
        echo %%A
        echo %%B
        set actualpingserver=%%B

        if /I "!fastestresponse:~,-2!" GTR "!actualpingserver:~,-2!" (
            set fastestresponse=%%B
            set fastestserver=%%A
        )
    )
)

REM @echo on
echo %fastestserver%
echo %fastestresponse%

endlocal

more info about delayed expansion

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.