Page 1 of 1

Assign Com Port to Variable

Posted: 06 May 2017 19:08
by jbgtenn
I was hoping someone could assist me in my batch file. I have been using this : wmic path win32_pnpentity get caption | find "COM" to list com ports and manually assign them to a variable.
set /p com="The device I am wanting#: "

Was hoping there was a way to automatically search for the com port number and assign it to %com% if I know the exact name I am looking for?

Re: Assign Com Port to Variable

Posted: 07 May 2017 04:10
by penpen
I wish i could help at once, but (sad to say):
There's not a single com port on any of my PCs.

So what is the output of:

Code: Select all

wmic path win32_pnpentity get caption | find "COM"



penpen

Re: Assign Com Port to Variable

Posted: 07 May 2017 11:45
by jbgtenn
penpen wrote:I wish i could help at once, but (sad to say):
There's not a single com port on any of my PCs.

So what is the output of:

Code: Select all

wmic path win32_pnpentity get caption | find "COM"



penpen



C:\temp>wmic path win32_pnpentity get caption | find "COM"
Example Serial Port 1 (COM219)
Example Serial Port 2 (COM220)




That is what the output looks like. Was hoping to specify the exact name I'm looking for such as : "Example Serial Port 2" and find the com port which is Com220 and put it in variable automatically.



I figured out how to display only the device name I am looking for with : wmic path win32_pnpentity where "caption like '%Example Serial Port 2%'" get caption |Find "COM"

This give will give the output as such : Example Serial Port 2 (COM220)

I'm Not sure how to extract just the number 220 and put in variable from the above command.

Re: Assign Com Port to Variable

Posted: 07 May 2017 12:41
by aGerman
You could execute it in a FOR /F loop

Code: Select all

@echo off &setlocal
for /f "tokens=2 delims=()" %%i in ('wmic path win32_pnpentity where "caption like 'Example Serial Port 2%%'" get caption') do set "com=%%i"
echo %com:~3%
pause

Steffen

Re: Assign Com Port to Variable

Posted: 07 May 2017 13:05
by jbgtenn
Thank You, It worked perfect !!!