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----------------------------------------------------
相关推荐
阿虎儿3 小时前
[实战记录] Windows 11 远程桌面已开启,但 3389 端口无监听?终极排查与修复
windows
Andy7 小时前
C++ list容器基本逻辑结构详解
c++·windows·list
扬帆破浪10 小时前
免费开源AI软件.桌面单机版,可移动的AI知识库,察元 AI桌面版:免费开源的AI软件首启动 FirstRunSetup向导背后做了什么
人工智能·windows·电脑·知识图谱
ITHAOGE1510 小时前
2026年Win7最终版ISO系统映像下载!(集成补丁、旗舰版、完整无精简、64位/32位可选、Windows 7、简体中文/繁体中文/英语可选)
windows·科技·microsoft·微软·电脑
yuanpan10 小时前
Python + PyAutoGUI 实战:Windows 自动化办公脚本开发入门
windows·python·自动化
扬帆破浪10 小时前
免费开源AI软件.桌面单机版,可移动的AI知识库,察元 AI桌面版:Windows装包被防病毒拦了 看安装日志和签名链的实战
人工智能·windows·开源·知识图谱
万邦科技Lafite10 小时前
API接口一键获取商品评论,根据商品评论分析客户画像
linux·服务器·数据库·windows·microsoft·电商开放平台
Kiling_070411 小时前
Java集合框架:List集合详解与应用
java·开发语言·windows
一个人旅程~11 小时前
如何永久关闭bitlocker并防止出现更新后被强制加密?
linux·windows·经验分享·电脑
csdn2015_11 小时前
java springboot 文件导入,判断第一列的值是否有重复
java·windows·spring boot