0

I have the following command that works pretty well:

FOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| grep :7010') DO TaskKill.exe /PID %%P /F

The issue is, if the same port is found multiple times with the same PID, my script returns errno 1 because the attempts to kill the PID after the first have failed.

Is there a way to modify the above so that it only attempts to kill the PID once?

MrDuk
  • 16,578
  • 18
  • 74
  • 133

1 Answers1

1

this might work for you:

@ECHO OFF &SETLOCAL disableDelayedExpansion
FOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| grep :7010') DO (
    IF NOT DEFINED PID.%%P (
        TASKKILL.exe /PID %%P /F
        SET "PID.%%P=7"
    )
)
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • This worked great - did you set PID.P to 7 arbitrarily? Also, can you elaborate on the need for disableDelayedExpansion? – MrDuk Mar 24 '14 at 15:44
  • `7` is _The Magical Batch Number_; but you can replace it :) `disableDelayedExpansion`: you can set the delayed expansion status in the registry or on the command line. I use this always to get a defined start environment. It is not necessary in this case. – Endoro Mar 24 '14 at 15:50