Windows下使用Bat文件杀死进程

输入端口号杀死进程

新建一个"kill port.bat"文件

复制代码
@echo off
rem open var delay
setlocal enabledelayedexpansion
rem recev var 
set /p port=请输入端口号
if "%port%" == "" (
    echo Please enter port
    goto end
)
echo ---------------start find who is using port %port%----------------------- 
rem Add colon before port and space after port to ensure no mistake
for /f "tokens=1-5" %%a in ('netstat -ano ^| find ":%port% "') do (
    rem a is protocol,b is local ip:port, c is remote ip:port,d is state or pid,e is pid or null 
    echo %%a %%b %%c %%d %%e
    rem Cut the %%b with the : to get the port
    for /f "delims=:, tokens=1-2" %%j in ("%%b") do (set destport=%%k)
    rem Delay variable in for loop must use! Symbol instead of% symbol
    echo The port found here is !destport!. What we need is %port%  
    rem If %%b cuts the same port as the target port
    if "!destport!" equ "%port%" (
        if "%%e" == "" (
            rem sometimes e is empty pid in d
            if not "%%d" == "0" (
                echo get d col pid %%d
                set pid=%%d
            )
        ) else (
            if not "%%e" == "0" (
               echo get e col pid %%e
               set pid=%%e
            )
        )
    ) else (
        echo this row local not use port %port% 
    )
)
echo ---------------end find who is using port %port%----------------------- 
if "%pid%"=="" (
 echo Port %port% is not in use 
) else (
 echo used port %port% the process %pid%
 taskkill /f /pid %pid%
)
pause

:end
echo ---------------end----------------------------------------------------
相关推荐
卷Java4 小时前
GPTQ vs AWQ vs GGUF:模型量化工具横向测评
开发语言·windows·python
x-cmd5 小时前
[260429] x-cmd v0.9.1:一键开启 DeepSeek-V4-Pro Max 模式 + 1M 上下文;顺手重构了 uuid 模块
windows·重构·uuid·claude·curl·x-cmd·deepseek-v4-pro
今夕资源网5 小时前
Windows 上安装 Claude Code并且接入DeepSeekV4-Pro的Max模式和激活1M上下文
windows
HLJ洛神千羽6 小时前
电脑右下角显示【周几或星期几】和【上下午】方法
windows
ITHAOGE156 小时前
下载 | Windows Server 2025官方原版ISO映像!(4月更新、标准版、数据中心版、26100.32690)
服务器·windows·科技·微软·电脑
专注VB编程开发20年8 小时前
Windows API 所有老式结构体4字节对齐,但是64位VBA,Twinbasic弄成了8字节对齐,大BUG
windows·bug
东篱把酒黄昏8 小时前
wsl和Windows混合开发高级配置详细指导
windows
helloliyh9 小时前
windows设置定时任务开机执行bat或exe文件
windows
清水白石0089 小时前
从“类型体操”到工程设计:用 Python 解释协变、逆变与不变
网络·windows·python
卷Java10 小时前
上下文压缩
开发语言·windows·python