@echo off
setlocal enabledelayedexpansion
set port=%1%
if "%port%" == "" (
echo Please enter port
goto end
)
rem Add colon before port and space after port to ensure no mistake
for /f "tokens=1-5" %%a in ('netstat -ano ^|find "LISTENING" ^| find ":%port% "') do (
echo %%a %%b %%c %%d %%e
@echo %%b|findstr /c":%port%" >nul
if %errorlevel% equ 0 (
if "%%e" == "" (
if not "%%d" == "0" (
echo port is localhost set pid %%d
set pid=%%d
)
) else (
if not "%%e" == "0" (
echo port is localhost set pid %%e
set pid=%%e
)
)
) else (
echo port not localhost
)
)
if "%pid%"=="" (
echo Port not used %port%
) else (
echo used port %port% the process %pid%
taskkill /f /pid %pid%
)
:end
echo end