Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 

Sign in to follow this  
Mosh

Using CMD to search for strings and return next character?

Recommended Posts

Mosh    0

Hi

I ask for help in extracting a particular substring from a set of strings in text files, I will show an example to better explain:

this is the txt file:

"

USB\VID_19D2&PID_1589&MI_03\7&110C67F3&0&0003               : ZTE UI AT Interface (COM3)
USB\VID_19D2&PID_1589&MI_04\7&110C67F3&0&0004               : ZTE Voice Device (COM4)
USB\VID_19D2&PID_1589&MI_05\7&110C67F3&0&0005               : ZTE Diagnostics Device (COM16)
USB\VID_2341&PID_0043\95634303432351E031F1                  : Arduino Uno (COM7)
ACPI\PNP0401\5                                              : ECP Printer Port (LPT1)
ACPI\PNP0501\0                                              : Communications Port (COM1)
USB\VID_23C1&PID_D314\FFFFFFFFFFFF51BA21D4                  : MightyBoard, S3G via Serial (COM8)
7 matching device(s) found.

"

the data i need from this text is the COM numbers for the USB ports only, and so, I would like to get an output text file which will contain the following text:

"3,4,16,7,8"

BTW, this is the output of DEVCON for mapping the USB ports.

Your help will be much appreciated.

Thanks

Mosh

Share this post


Link to post
Share on other sites
Wireless    0

Tested with your sample data in input.txt.  Results are written to output.txt.

(set s=)
for /f "tokens=2 delims=()" %%A in ('findstr /i /r "^USB..*(COM[0-9][0-9]*)$" input.txt') do (call set "s=%%s%%,%%A" & call set "s=%%s:COM=%%")
if defined s (echo "%s:~1%") > output.txt

Share this post


Link to post
Share on other sites
jaclaz    927

That is the output of

devcon listclass ports

, right?

Maybe you could do instead:

devcon listclass USB |FIND "(COM"

This way it is already a list of USB COM devices only. :unsure:

jaclaz

Share this post


Link to post
Share on other sites
Mosh    0


Thanks Wireless

I tried your code but didnt work, any idea why?

jaclaz

i need the active USB ports and I use:

devcon.exe /Find =Ports >out.txt 

 

Share this post


Link to post
Share on other sites
jaclaz    927

I will try again. :)

What is the output on your setup of running this command:

devcon listclass ports

is it different from the one you obtain with devcon.exe /Find =Ports ? (actually "devcon find =Ports" should work just the same, no need for the slash)

And what is the output of this one?:

devcon listclass USB |FIND "(COM"

The snippet Wireless posted is NOT to be issued on command line, but rather copied to a batch file that later is executed, *like*:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
(set s=)
for /f "tokens=2 delims=()" %%A in ('findstr /i /r "^USB..*(COM[0-9][0-9]*)$" input.txt') do (call set "s=%%s%%,%%A" & call set "s=%%s:COM=%%")
REM if defined s (echo "%s:~1%") > output.txt
if defined s (echo "%s:~1%")

If you want (why?) run it on command line you need to "unescape" the %%A variable, but most probably some more tricks will be needed for the expansion of the variables.

Generally speaking it is not a good idea to save the output of console to a text file that then needs to be re-parsed, it  makes a lot more sense to run the command in a batch and directly parse its output, without writing temporary files.

jaclaz 

Edited by jaclaz

Share this post


Link to post
Share on other sites
Mosh    0

Hi

thanks for the help here, but I will incorporate phyton to solve this as this also runs in my project in parallel.

 

Share this post


Link to post
Share on other sites
Yzöwl    30

You could probably do this directly using this GetCOMs.cmd file Run as administrator:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_="
FOR /F "SKIP=1TOKENS=2DELIMS=M" %%A IN ('WMIC /NAMESPACE:\\ROOT\WMI PATH^
 MSSERIAL_PORTNAME GET PORTNAME') DO FOR %%B IN (%%A) DO SET "_=!_!,%%B"
IF DEFINED _ (IF "%_:~,1%"=="," SET "_=%_:~1%"
	ECHO/"!_!">"%~dp0AllCOMs.txt")
PAUSE

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×