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----------------------------------------------------
相关推荐
无巧不成书02182 小时前
Rust开发环境完全指南:Windows/Linux双平台配置与实战
linux·windows·rust·gnu·msvc·mingw-w64安装·镜像配置
xiaoshuaishuai82 小时前
C# ZLibrary数字资源分发
开发语言·windows·c#
私人珍藏库3 小时前
[Windows] 电子教鞭演示工具 PointerStick v7.11
windows·自动化·工具·软件·多功能
Full Stack Developme5 小时前
Hutool TreeUtil 教程
大数据·windows
破阵子443285 小时前
PowerShell-7 下载安装教程(如何更新 PowerShell)
windows
安当加密6 小时前
指纹一按,安全上线:指纹登录如何为生产线 Windows 电脑实现低成本防勒索?
windows·安全·电脑
不做超级小白6 小时前
解密Win11的Win+S搜索:从索引原理到自启动的攻防战
windows·安全
卵男(章鱼)6 小时前
系统终端命令对比大全(Linux发行/macOS/Windows)
linux·运维·服务器·windows·macos
songjunwei16 小时前
MFC应用程序是文档/视图结构支持的时候对工具栏加载图片的问题
windows
余衫马6 小时前
在 Windows 服务中托管 ASP.NET Core Web API (.net6)
运维·windows·后端·asp.net·.net