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----------------------------------------------------
相关推荐
lxlmycsdnfree27 分钟前
程序员必知的 89 个操作系统核心概念
windows
西邮彭于晏5 小时前
差分进化算法
windows·python·算法
分享者花花9 小时前
恢复出厂设置后如何从 iPhone 恢复数据
windows·macos·ios·智能手机·excel·cocoa·iphone
苦藤新鸡10 小时前
用网络编程完成windows和linux跨平台之间的通信(服务器)
linux·网络·windows
雪 狼13 小时前
unity对于文件夹的操作
windows·unity·游戏引擎
爱分享的码瑞哥20 小时前
Rust 进阶教程
开发语言·windows·rust
初学️计算1 天前
网络协议与标准
运维·服务器·windows
ac-er88881 天前
win10使用小技巧二
windows·技巧·虚拟键盘·防止复制·安装字体
日出等日落1 天前
Windows系统安装分布式搜索和分析引擎Elasticsearch与远程访问详细教程
windows·分布式·elasticsearch
胡子洲1 天前
Windows电脑PC使用adb有线跟无线安装apk包
windows·adb·电脑