I am creating a batch file, in which I want to store the value returned by the following command (in Windows):
netstat -an | find ":port" /c
How to store the count value and print using echo?
I am creating a batch file, in which I want to store the value returned by the following command (in Windows):
netstat -an | find ":port" /c
How to store the count value and print using echo?
To capture the output of a command, you can use the for /F
command; to store it into a variable, use set
in the body of the for
loop:
for /F "delims=" %L in ('netstat -an ^| find ":port" /c') do (set "VAR=%L")
Note the escaped pipe ^|
. To use this within a batch file, replace %L
by %%L
.
This only works for a single-line output. If a command returns a multi-line output, only the last line is stored in variable VAR
.
I would guess something similar to:
@echo off
set "Blah=netstat -an|find ":port" /c"
echo %Blah%
pause