CMD上的批处理脚本以获取变量上的COM端口 [英] Batch Script on CMD to get COM port on a variable

查看:982
本文介绍了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.

推荐答案

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

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

@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! 

  • :::或::
  • :: or ::

@echo off && setlocal enabledelayedexpansion 
for /f "tokens=2delims=COM:" %%i in ('mode^|findstr /C:"COM"')do set "_com=%%i"&echo/!_com!

:: 批处理结果 3 ::

:: batch result 3 ::

糟糕:对于命令行,请使用: 用于/f"tokens = 2delims = COM:" %% i('mode ^ | findstr"COM"')确实设置"_com =%i"&致电echo/%_ com%

Obs.: For command line use: for /f "tokens=2delims=COM:" %%i in ('mode^|findstr "COM"')do set "_com=%i"& call echo/%_com%


纯批处理解决方案...

Pure batch solution...

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

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

通过 chgport :

By chgport:

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

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

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

by using this variable: %%L to get n

@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

::命令行结果::

:: Commandline result ::

COM[n]:  DEVICE[id]:
COM3  =  \Device\huawei_cdcacm_AcmSerial1
COM4  =  \Device\huawei_cdcacm_AcmSerial0
 3 = COM3
 4 = COM4

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

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

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

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

@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!

::命令行结果::

:: Commandline result ::

COM4 \Device\huawei_cdcacm_AcmSerial0
COM3 \Device\huawei_cdcacm_AcmSerial1
4 = COM4
3 = COM3

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

Sorry, English isn’t my first language..

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆