Search code examples
batch-fileechofindstr

Echo %var% prints FINDSTR parameters


I'm currently making a bat script, to ping a host, and return the Packetloss percentage, and the avg ms.

Im using for /f to get the results.

Only thing is, the packet loss and the avg ms is on two different lines.

Therefor, i'm using subcalls, and findstr to set a variable for each.

But, when im trying to call the variable again, it includes all the parameters for the findstr command.

What can i do?

Here's the code:

@echo off
SETLOCAL
SET PingCMD=ping.exe -w 300 -n 1 127.0.0.1
FOR /f "tokens=3,6,9,10,11,12 delims=," %%A IN ('%PingCMD%') DO (

    call :GetLoss %%A
    call :GetAvg %%A

)
echo PacketLoss %loss%  Average %avg%
pause


:GetLoss
SET loss=%1 %2 %3 %4 ^| findstr "loss tab"
GOTO :eof

:GetAvg 
SET avg=%1 %2 %3 ^| findstr "Average Gennemsnit average gennemsnit"
GOTO :eof
EndLocal

Solution

  • @ECHO OFF
    SETLOCAL
    SET PingCMD=ping.exe -w 300 -n 1 127.0.0.1
    FOR /f "tokens=3,6,9,10,11,12 delims=," %%A IN ('%PingCMD%') DO (
    
        call :GetLoss %%A
        call :GetAvg %%A
    
    )
    echo PacketLoss %loss%  Average %avg%
    PAUSE
    GOTO :eof
    
    
    :GetLoss
    IF "%1"=="Lost" SET loss=%2
    GOTO :eof
    
    :GetAvg 
    IF "%1"=="Average" SET avg=%2
    GOTO :EOF
    

    Translation's your baby...