0

I know this is a duplicate in a way but the answer in Get some information from netstat -e in new batch file wont work on my batch. i know i'm missing something maybe its the OS i'm using (Microsoft Windows XP Professional/X86-based PC)i'm just not sure what it is.

netstat -e | find "Bytes"

works fine but

for /F "tokens=2,3" %a in ('netstat -e ^| find "Bytes"') do echo received:%a sent:%b

and

for /F "tokens=2,3" %a in ('netstat -e ^| find "Bytes"') do set received=%a&set sent=%b

wont work. What i need is just the number from received:74546759 sent:8593498 (74546759) to become a echo listed in the command line so i can set it as a variable in this batch. what i need to do is show the "Bytes" as Bytes received in a numerical value so i can set it as %R1% & %R2% If someone could have patience and fix my batch i would be a happy camper. thanks so much for any help. MANY THANKS to MC ND for his answer it worked perfectly now i have the problem of how to differentiate between the first received and the second to do the math. %received% always gives the last value and then the Kbs/s are always 0

@echo off
title command line/batch echo totalbytes sent and received in netstat /e
title https://stackoverflow.com/questions/15382742/get-some-information-from-netstat-e-in-new-batch-file
:begin
color 0b
MODE CON:COLS=13 LINES=15
netstat -e | find "Bytes" >NUL
for /F "tokens=2,3" %%a in ('netstat -e ^| find "Bytes"') do set "received=%%a" & set "sent=%%b"
echo %received%
sleep 1
netstat -e | find "Bytes" >NUL
for /F "tokens=2,3" %%a in ('netstat -e ^| find "Bytes"') do set "received=%%a" & set "sent=%%b"
echo %received%
set /a R1=%received%
if %R1% == %R1% goto R1confirm
:R1confirm
set /a R2=%received%
if %R2% == %R2% goto R2confirm
:R2confirm
set /a Bytes1=%R1%
if %Bytes1% == %Bytes1% goto Bytes1confirm
:Bytes1confirm
set /a Bytes2=%R2%
if %Bytes2% == %Bytes2% goto Bytes2confirm
:Bytes2confirm
set /a varia1=%Bytes2%-%Bytes1%
set /a answer1=%varia1%
echo.
echo = %answer1% B/s
echo.
set /a varia2=%answer1%/1024
set /a answer2=%varia2%
echo = %answer2% Kbs/s
echo.
set /a varia2=%answer1%/1048576
set /a answer2=%varia2%
echo = %answer2% Mbs/s
echo.
pause
goto begin
Community
  • 1
  • 1
Calib
  • 95
  • 5

1 Answers1

0

Both for commands works from command line. To use it inside a batch file, double the percent signs

for /F "tokens=2,3" %%a in ('netstat -e ^| find "Bytes"') do set "received=%%a" & set "sent=%%b"
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • this worked perfectly how to i separate the two %received% to do the math for Kbs/s sorry i didn't think of that in the beginning. http://stackoverflow.com/users/2861476/mc-nd – Calib Apr 24 '14 at 15:15
  • In the first `for` assign the data to `%received1%`, in the second to `%received2%`, as you are waiting 1 second between the two reads, substract and you will have B/s. Remember batch files arithmetic is limited to values below 2147483648. – MC ND Apr 24 '14 at 15:43