本文介绍了CMD上的批处理脚本以获取变量上的COM端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储可用的COM端口的信息并将它们存储在变量中.

I want to store the information of my available COM ports and store them on a variable.

该命令必须在脚本中运行.仅搜索,我发现此命令非常有效:

The command must run in a script. Searching only I found this command which works perfectly:

wmic path win32_pnpentity get caption /format:table| find "COM"

它返回:

USB-SERIAL CH340 (COM6)

我希望它识别哪个设备.在此信息上,我需要提取COM66.

Which is the device I want it to recognize. On this Information I need to extract COM6 or 6.

推荐答案

更新:这是简短版本,仅用于获取

Update: This is the more short version for to get only the part from

@echo off && setlocal enabledelayedexpansion
for /f "tokens=2delims=COM:" %%i in ('mode ^| findstr /RC:"\C\O\M[0-9*]"') do set "_com=%%i" & echo/!_com!
@echo off && setlocal enabledelayedexpansion
for /f "tokens=2delims=COM:" %%i in ('mode^|findstr /C:"COM"')do set "_com=%%i"&echo/!_com!

:: 批处理结果 ::

:: batch result ::


对于bat文件,可能建议使用 chgport Reg Query

for bat file, may suggest use chgport to do this or Reg Query

通过 chgport :

By chgport:

通过使用以下变量: !_com_[%%L]! 获得 +

by using this variable: !_com_[%%L]! to get +

通过使用以下变量: %%L 获得

by using this variable: %%L to get

@echo off & setlocal enabledelayedexpansion & set "_cnt=0"
cd /d "%systemroot%"
for /f "tokens=* delims= " %%C in ('where /r . chgport.exe') do set "_chgport=%%~fC"
for /f "delims== tokens=1,2" %%i in ('!_chgport! ^| findstr /v "#"') do set /a "_cnt+=1" && set "_com_[!_cnt!]=%%i ^= %%j"
popd & echo/COM[n]:  DEVICE[id]: & for /l %%L in (1 1 !_cnt!) do echo/!_com_[%%L]!

:: for get number only :: By using %%C**
for /l %%L in (1 1 !_cnt!) do for /f %%C in ('echo/!_com_[%%L]:COM^=!') do set "_com_N=%%C" & echo/ !_com_N! = COM%%C
COM[n]:  DEVICE[id]:
COM3  =  \Device\huawei_cdcacm_AcmSerial1
COM4  =  \Device\huawei_cdcacm_AcmSerial0
 3 = COM3
 4 = COM4

通过 Reg Query :通过使用以下变量: !_com#! 来获取 +

By Reg Query :by using this variable: !_com#! to get +

通过使用以下变量: !_com#:COM=! 获得

by using this variable: !_com#:COM=! to get

@echo off & setlocal enabledelayedexpansion
set _key="HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM"
for /f "tokens=1,3 delims= " %%i in ('reg query !_key! ^| find /v "HKEY"') do (
   set "_device=%%i" & set "_com#=%%j" & echo/!_com#! !_device! )

:: for get number only :: By using !_com_N:~3! to get [n]
for /f "tokens=3 delims= " %%c in ('reg query !_key! ^| find /v "HKEY"') do set "_com_N=%%c"&& echo/!_com_N:~3! = !_com_N!
COM4 \Device\huawei_cdcacm_AcmSerial0
COM3 \Device\huawei_cdcacm_AcmSerial1
4 = COM4
3 = COM3

对不起,英语不是我的母语.

Sorry, English isn’t my first language..

这篇关于CMD上的批处理脚本以获取变量上的COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:37