0

This is my code :

@echo off
netstat -a -n | find /c "127.0.0.1:80"
pause

It return 1 Value in my CMD. i want to make IF Condition, for example, if The return value is 1 do this, if 0 do this. can you help me guys?

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Galih laras prakoso
  • 689
  • 2
  • 7
  • 22
  • Related: https://stackoverflow.com/questions/14691494/check-if-command-was-successfull-in-a-batch-file – Azeem Jul 21 '17 at 04:35

2 Answers2

0

Wrap the command in a for /f parsing the output.

@echo off
For /f %%A in ('netstat -a -n ^| find /c "127.0.0.1:80"') Do Set Count=%%A
If %Count% equ 0 (
  echo Count = 0 do this
) Else (
  echo Count not 0 do that
)
pause
0

You could probably just use this construct:

NetStat -na | Find "127.0.0.1:80" >Nul && (
    Echo Found
) || (
    Echo Not found
)

Change Echo Found to the command(s) you need for one or more matches and Echo Not found to the command(s) you need for no connection matches a little like this:

Depending upon your specific requirements you could possibly replace -na with -np TCP

BTW your script is returning the value from find not from netstat.

Compo
  • 36,585
  • 5
  • 27
  • 39