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----------------------------------------------------
相关推荐
桌面运维家6 小时前
Windows权限管理进阶:UAC配置与安全策略实战
windows
桌面运维家10 小时前
BGP路由优化实战:加速收敛,提升网络稳定性
网络·windows·php
m0_7381207212 小时前
我的创作纪念日0328
java·网络·windows·python·web安全·php
桌面运维家13 小时前
VHDX父盘定位:相对路径与Windows盘符变更难题
windows
Lyyaoo.13 小时前
【JAVA基础面经】JAVA的面向对象特性
java·开发语言·windows
FPGA的花路14 小时前
Windows10/11永久关闭自动更新
windows·自动更新·永久关闭
水饺编程14 小时前
第4章,[标签 Win32] :SysMets3 程序讲解01
c语言·c++·windows·visual studio
私人珍藏库14 小时前
[Windows] Cap 0.4.81
windows·工具·录屏·软件·多功能
想你依然心痛14 小时前
从零开始:Mac/Windows/Linux 三系统开发环境配置完全指南
linux·windows·macos
ths51215 小时前
doris 中 array_agg函数用法总结
windows