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----------------------------------------------------
相关推荐
美酒没故事°19 小时前
Open WebUI安装指南。搭建自己的自托管 AI 平台
人工智能·windows·ai
一个欠登儿程序员21 小时前
在国产服务器上通过 Docker 部署 Windows 虚拟机
服务器·windows·docker
爱宇阳1 天前
WSL2 隔离 Windows PATH 实战指南
windows·环境变量
ALex_zry1 天前
C++模板元编程实战技巧
网络·c++·windows
I疯子1 天前
2026-04-08 打卡第 5 天
开发语言·windows·python
一个人旅程~1 天前
旧笔记本电脑安装win10精简版LTSB&win10LTSC&linuxmint作为三系统的操作指导书(以DELL n4020为例)
linux·windows·经验分享·电脑
开开心心就好1 天前
支持自定义名单的实用随机抽签工具
windows·计算机视觉·计算机外设·excel·散列表·启发式算法·csdn开发云
dyj0951 天前
OpenClaw小龙虾本地部署【Windows系统 + 接入飞书】
windows·飞书
CresCent_Charles1 天前
(已解决)踩坑记录:Windows 11安装pointops编译时报错
windows
skywalk81631 天前
Windows下安装编译安装Whisper-CPP:一个语音实现框架集和高性能推理模型
人工智能·windows·whisper