Reputation: 13
I'm trying to make a batch file which performs a ping and stores some of the results as variables. I'm trying to store packets sent, received, lost and latency minimum, maximum average.
I've managed to do this, albeit with separate ping commands so the results in each variable are from separate data sets and not relevant to each other:
@ECHO OFF
FOR /F "tokens=4 delims=, " %%A IN ('ping -n 2 8.8.4.4 ^| findstr /R "^Packets: Sent =.$"') DO SET SENTPACKETS=%%A
ECHO Packets sent: %SENTPACKETS%
FOR /F "tokens=7 delims=, " %%A IN ('ping -n 2 8.8.4.4 ^| findstr /R "^Packets: Sent =.$"') DO SET RECEIVEDPACKETS=%%A
ECHO Packets received: %RECEIVEDPACKETS%
FOR /F "tokens=10 delims= " %%A IN ('ping -n 2 8.8.4.4 ^| findstr /R "^Packets: Sent =.$"') DO SET LOSTPACKETS=%%A
ECHO Packets lost: %LOSTPACKETS%
FOR /F "tokens=4 delims=,m " %%A IN ('ping -n 2 8.8.4.4') DO SET MINIMUMLATENCY=%%A
ECHO Minimum latency: %MINIMUMLATENCY%
FOR /F "tokens=9 delims=,m " %%A IN ('ping -n 2 8.8.4.4') DO SET MAXIMUMLATENCY=%%A
ECHO Maximum latency: %MAXIMUMLATENCY%
FOR /F "tokens=13 delims=,m " %%A IN ('ping -n 2 8.8.4.4') DO SET AVERAGELATENCY=%%A
ECHO Average latency: %AVERAGELATENCY%
pause
So to try and remedy this and have the variables all collected from the same ping command, I've tried storing the results in a .txt file like so:
@echo off
ping 8.8.4.4>c:\data.txt
FOR /F "tokens=3 delims= " %%A IN (c:\data.txt) DO SET SENTPACKETS=%%A
ECHO Packets sent: %SENTPACKETS%
FOR /F "tokens=7 delims= " %%A IN (c:\data.txt) DO SET RECEIVEDPACKETS=%%A
ECHO Packets received: %RECEIVEDPACKETS%
FOR /F "tokens=10 delims= " %%A IN (c:\data.txt) DO SET LOSTPACKETS=%%A
ECHO Packets lost: %LOSTPACKETS%
FOR /F "tokens=4 delims=,m " %%A IN (c:\data.txt) DO SET MINIMUMLATENCY=%%A
ECHO Minimum latency: %MINIMUMLATENCY%
FOR /F "tokens=9 delims=,m " %%A IN (c:\data.txt) DO SET MAXIMUMLATENCY=%%A
ECHO Maximum latency: %MAXIMUMLATENCY%
FOR /F "tokens=13 delims=,m " %%A IN (c:\data.txt) DO SET AVERAGELATENCY=%%A
ECHO Average latency: %AVERAGELATENCY%
pause
I know I've got the min, max, avg latency figures correct and maybe number of packets lost (I'm not certain on that one) but the number of packets sent and received are not being read from the correct 'tokens' and no matter what I try, I can't seem to get it to read from the correct line in data.txt
Any ideas?
Upvotes: 0
Views: 1143
Reputation:
@Echo off
:: clear variables
For %%A in (LatMin LatMax LatAvg PacSnt PacRcv PacLos) Do Set "%%A="
:: do the ping
For /f "tokens=1-8 delims=:=, " %%A in (
'ping 8.8.4.4 2^>Nul ^| findstr "Pa Min"'
) Do If "%%A"=="Packets" (
SET PacSnt=%%C
SET PacRcv=%%E
SET PacLos=%%G
) Else If "%%A"=="Minimum" (
SET LatMin=%%B
SET LatMax=%%D
SET LatAvg=%%F
)
ECHO Packets sent : %PacSnt%
ECHO Packets received: %PacRcv%
ECHO Packets lost : %PacLos%
ECHO Minimum latency : %LatMin:~0,-2%
ECHO Maximum latency : %LatMax:~0,-2%
ECHO Average latency : %LatAvg:~0,-2%
Upvotes: 0
Reputation: 13
Magoo's code worked great!
@echo off
ping -n 100 8.8.4.4>c:\data.txt
for /f "tokens=4 delims=, " %%A IN ('findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul') do set sentpackets=%%A
echo Packets sent: %sentpackets%
for /f "tokens=7 delims=, " %%A IN ('findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul') do set receivedpackets=%%A
echo Packets received: %receivedpackets%
for /f "tokens=10 delims= " %%A IN ('findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul') do set lostpackets=%%A
echo Packets lost: %lostpackets%
for /f "tokens=4 delims=,m " %%A IN (c:\data.txt) do set minimumlatency=%%A
echo Minimum latency: %minimumlatency%
for /f "tokens=9 delims=,m " %%A IN (c:\data.txt) do set maximumlatency=%%A
echo Maximum latency: %maximumlatency%
for /f "tokens=13 delims=,m " %%A IN (c:\data.txt) do set averagelatency=%%A
echo Average latency: %averagelatency%
pause
Unfortunately I couldn't get LotPings' code to work but I appreciate the response. Thank You.
Upvotes: 0
Reputation: 79983
First, use
@echo off
>c:\data.txt ping 8.8.4.4
as a numeric directly before a redirector redirects that standard device number, not stdout
Then replace the filename alone in the first 3 items with
'findstr /R "^Packets: Sent =.$" c:\data.txt'
Which selects the appropriate line from the file. You'd need to ensure that the tokens
and delims
option match those in your first posted routine (the first one uses tokens 4
and delims ,
but your second routine uses tokens 3
and delims as a space alone) - that is, assuming your first code works satisfactorily.
Obviously, your filters and selections for the first 3 data items have different filters, so this needs to be applied to all entries.
Next, add 2^>nul
into the command to be executed 'findstr /R "^Packets: Sent =.$" c:\data.txt 2^>nul'
to suppress nasty messages and remember that if you initialise the variables you use first, then if the file fails to appear in the file, the value initially assigned to the variable is the "none found" message.
Oh, and try editing your post to include a typical ping
result and resultant report together with a description of how the report should read.
Upvotes: 1