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----------------------------------------------------
相关推荐
kakakahahahaha14 小时前
Windows笔记本重装系统的4种路径和风险边界
windows·电脑·笔记本电脑·内容运营·软件需求·重装系统
自动化监测Learner18 小时前
Windows 自带搜索太慢?Everything 安装与使用指南,全盘文件秒搜
windows·everything
天机玄正21 小时前
云运维高阶二:CLI 调用 API (白名单追随)
windows·云原生
您^_^21 小时前
DeepSeek-Harness v0.1.5-alpha.1更新后旧会话打不开?5 步抢救 8MB 会话照抄
人工智能·windows·个人开发·deepseek v4 pro·deepseekharness
正仪1 天前
systemd 是什么?
linux·运维·windows
小溪学编程1 天前
Java ListIterator 接口详解:双向遍历与列表修改的利器
java·开发语言·windows
2601_962364971 天前
没有 TPM2.0,还能用 Windows Hello 摄像头吗?
windows·安全·电脑
江湖人称菠萝包1 天前
【Windows】《深入浅出Windows API程序设计:编程基础篇》笔记-Chapter9-对话框
windows·笔记
问简1 天前
Windows11自定义分区大小安装系统
windows
sukalot1 天前
windows 驱动实例分析系列: HidHide驱动分析-HidHideClient 篇(中)
windows·驱动开发