@echo off
setlocal
set /p port="input port to kill: "
echo Stopping program running on port %port%...
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :%port%') do (
taskkill /T /F /PID %%a
echo Program running on port %port% has been stopped.
goto done
)
echo No program running on port %port%.
:done
pause
"tokens=5"
参数告诉 for 命令只提取每行输出的第 5 个字段(即 PID 字段)。
^|
中的^
为转义符。
goto done
实现只获取第一行输出的 PID(按第一行输出的 PID 结束进程)。
标签:bat,PID,port%,端口,指定,echo,running,done,port From: https://www.cnblogs.com/Higurashi-kagome/p/17655071.html参考:ChatGPT