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----------------------------------------------------
相关推荐
AC赳赳老秦1 小时前
OpenClaw多平台部署:Windows+Linux跨系统协同,实现全场景覆盖
linux·服务器·前端·网络·windows·deepseek·openclaw
feVA LTYR2 小时前
Windows上安装Go并配置环境变量(图文步骤)
开发语言·windows·golang
加号32 小时前
Windows10 免密码/空密码实现远程桌面连接:完整配置指南
windows
longerxin20203 小时前
Windows 修改 DNS 提示 “出现了一个意外的情况,不能完成所有你在设置中所要求的更改”?用这行命令直接搞定
windows
x***r1514 小时前
procexp64使用步骤详解(附进程分析与句柄查看教程)
windows
2501_939998206 小时前
2026.4.17 更新|一键获取 Win11 25H2 镜像文件
windows
xinhuanjieyi7 小时前
windows安装vscode服务端
ide·windows·vscode
叶之香7 小时前
探究 libhv Socketpair 在 clumsy 模拟延迟下的“超时”之谜
windows·websocket
拾薪7 小时前
【hermes】windows 安装hermes,配置glm
windows·安装·install·win·hermes·wins
雪域迷影8 小时前
Windows上使用VS2026和CMake编译LearnOpenGL项目源代码
windows·cmake·opengl·vs2026·gthub