Assign Com Port to Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jbgtenn
Posts: 5
Joined: 11 Feb 2017 13:01

Assign Com Port to Variable

#1 Post by jbgtenn » 06 May 2017 19:08

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?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Assign Com Port to Variable

#2 Post by penpen » 07 May 2017 04:10

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

jbgtenn
Posts: 5
Joined: 11 Feb 2017 13:01

Re: Assign Com Port to Variable

#3 Post by jbgtenn » 07 May 2017 11:45

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Assign Com Port to Variable

#4 Post by aGerman » 07 May 2017 12:41

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

jbgtenn
Posts: 5
Joined: 11 Feb 2017 13:01

Re: Assign Com Port to Variable

#5 Post by jbgtenn » 07 May 2017 13:05

Thank You, It worked perfect !!!

Post Reply