Batch Script on CMD to get COM port on a variable

2019-07-31 04:24发布

问题:

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" 

It returns:

USB-SERIAL CH340 (COM6)

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

回答1:

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!

:: batch result 3 ::

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...

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

By chgport:

by using this variable: !_com_[%%L]! to get COM+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

By Reg Query : by using this variable: !_com#! to get 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..



回答2:

Here is a way to do it in a cmd .bat script file. This will result in the last com port being set. I do not see any indication as to how multiple com ports are to be handled.

@ECHO OFF
FOR /F "delims=^T tokens=1,2" %%a IN ('powershell -NoLogo -NoProfile -Command ^
    "Get-CimInstance -ClassName Win32_PnPEntity |" ^
        "Where-Object { $_.Name -match '.*\(COM(\d)\)'} | Out-Null;" ^
        "$Matches[0] + '^t' + $Matches[1]"') DO (
    SET "CPNAME=%%a"
    SET "CPNUM=%%b"
)

ECHO CPNAME is %CPNAME%
ECHO CPNUM is %CPNUM%

It is, of course, easier without the cmd.exe overhead if using PowerShell directly. You could save the following code in comport.ps1.

Get-CimInstance -ClassName Win32_PnPEntity |
    Where-Object { $_.Name -match '.*\(COM(\d)\)' } | Out-Null
$cpname = $Matches[0]
$cpnum = $Matches[1]

$cpname
$cpnum


回答3:

You will need a for /F loop to parse the output of this command. Even though this will not work, because my computer output Intel(R)*(COM3), we talk about your computer and a specific output. I suggest you to use:

for /F "tokens=2 delims=()" %%A IN ('wmic path win32_pnpentity get caption /format:table ^| findstr /c:"COM"') do echo %%A

This, will echo the information. To store it in a variable, use set "com_info=%%A".

You said also, that you would like to extract 6 from COM6. This is also possible; use:

for /F "tokens=2 delims=()" %%A IN ('wmic path win32_pnpentity get caption /format:table ^| findstr /c:"COM"') do set "com_info=%%A"&call set "com_num=%com_info:~-1%"

Replace echo with set or the opposite where you want to echo and the opposite.

In cmd, replace %%A with %A where presented.