Windows script package to print error message if port used

I am trying to write a batch script that errors if port 1099 is already in use.

Unfortunately, I have to write it to a DOS script package (I cannot install anything).

I know that I can manually print the PID of the hogging port of process 1099:

netstat -aon | findstr ":1099" 

But I want to be able to run this command in a batch script and exit the script with an error message if this command has any output.

I assume that when I clicked, I could redirect the output to a temporary file and check its size, but this seems really hacked ...

+6
windows dos port batch-file
source share
1 answer
  netstat -an | FINDSTR ":1099" | FINDSTR LISTENING && ECHO Port is in use && EXIT 1 

You can use && in a script package to run a command only if the previous command was successful (based on the exit code / ERRORLEVEL ). This allows you to display a message and exit only if the search string is found in netstat output.

Also, you want to explicitly look for LISTENING ports.

FINDSTR supports regular expressions, so you can also do the following to shorten the command line:

 netstat -an | findstr /RC:":1099 .*LISTENING" && ECHO Port is in use && EXIT 1 
+11
source share

All Articles