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
.
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.
It is, of course, easier without the cmd.exe overhead if using PowerShell directly. You could save the following code in
comport.ps1
.Update: This is the more short version for to get only the N part from COM3
:: batch result 3 ::
for bat file, may suggest use
chgport
to do this orReg Query
By
chgport
:by using this variable:
!_com_[%%L]!
to get COM+nby using this variable:
%%L
to get nBy
Reg Query
: by using this variable:!_com#!
to get COM+nby using this variable:
!_com#:COM=!
to get nSorry, English isn’t my first language..
You will need a
for /F
loop to parse the output of this command. Even though this will not work, because my computer outputIntel(R)*(COM3)
, we talk about your computer and a specific output. I suggest you to use:This, will
echo
the information. To store it in a variable, useset "com_info=%%A"
.You said also, that you would like to extract
6
fromCOM6
. This is also possible; use:Replace
echo
withset
or the opposite where you want toecho
and the opposite.In cmd, replace
%%A
with%A
where presented.