0

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Sid ABS
  • 59
  • 1
  • 11
  • 1
    Possible duplicate of [Capture output command CMD](http://stackoverflow.com/questions/14646575/capture-output-command-cmd). Also, you may find this useful: http://blogs.msdn.com/b/oldnewthing/archive/2012/07/31/10334556.aspx – Ryan Bemrose Oct 26 '15 at 06:35

2 Answers2

2

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

I would guess something similar to:

@echo off
set "Blah=netstat -an|find ":port" /c"
echo %Blah%
pause
Potato Head
  • 143
  • 1
  • 8