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----------------------------------------------------
相关推荐
孫治AllenSun2 小时前
【算法】图相关算法和递归
windows·python·算法
TeleostNaCl2 小时前
解决 Chrome 无法访问网页但无痕模式下可以访问该网页 的问题
前端·网络·chrome·windows·经验分享
寒某6 小时前
在Windows上部署RAGFlow
windows·ai
安全不再安全7 小时前
免杀技巧 - 早鸟注入详细学习笔记
linux·windows·笔记·学习·测试工具·web安全·网络安全
仰望—星空10 小时前
MiniEngine学习笔记 : DescriptorHeap
windows·笔记·学习
FreeBuf_11 小时前
研究人员披露 Windows SMB 服务器权限提升漏洞(CVE-2025-58726)
运维·服务器·windows
hjh200514 小时前
在Windows系统上部署 CosyVoice 2
windows·cosyvoice 2
小白一键重装系统15 小时前
新手从零开始学电脑,0元学会重装系统
windows·电脑·重装系统·小白一键重装系统
私人珍藏库15 小时前
[Windows] 漫画翻译工具Saber Translator2.5.1
windows·动画·工具·翻译
仰望—星空15 小时前
MiniEngine学习笔记 : RootSignature
windows·笔记·学习