Parsing command output into variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
l95theses
Posts: 4
Joined: 17 Jan 2011 08:14

Parsing command output into variable

#1 Post by l95theses » 17 Jan 2011 08:25

I'm trying to use devcon.exe to output command information into this batch file. Once this is accompished I try to split the data it gives me using "(" delimiter and I would like to further split it so that I can get the information behind this character ")" but my variable wants to be two lines even though i use a token number that should take out the other line. The only way I can separate the line number is by using "/p" before my set command and what is weird is that it still echos the data when I never said to echo I only told it to set a variable. Here is my code and I will attach the output of the code based on the fact that some of you may not have devcon.exe. The photo attached is the output of the code before it is parsed. However when I run this code it just echos "COM5)", but i need a single variable that just says "COM5" or whatever com number it happens to be.

Code: Select all

@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
FOR /F "tokens=2 delims=(" %%a IN ('"%~dp0devcon.exe find =ports *VID_10C4^&PID_EA60*"') DO (
set /p comnumber=%%a
)
pause

Image
Thanks in advance!

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

Re: Parsing command output into variable

#2 Post by aGerman » 17 Jan 2011 09:34

What about delims=() :?:

Regards
aGerman

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Parsing command output into variable

#3 Post by ChickenSoup » 17 Jan 2011 09:39

It is hard to guess exactly without knowing the variations of the output. But for this output something like this should work:

Code: Select all

@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
FOR /F "tokens=2 delims=()" %%a IN ('"%~dp0devcon.exe find =ports *VID_10C4^&PID_EA60*" ^| find "COM"') DO set comnumber=%%a
echo %comnumber%
pause

l95theses
Posts: 4
Joined: 17 Jan 2011 08:14

Re: Parsing command output into variable

#4 Post by l95theses » 17 Jan 2011 09:57

The above example works of:

Code: Select all

@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
FOR /F "tokens=2 delims=()" %%a IN ('"%~dp0devcon.exe find =ports *VID_10C4^&PID_EA60*"') DO (
echo comnumber=%%a
)
pause

However, I cannot use comnumber variable anywhere else outside the loop. It returns as a two line thing as you will see the output below.
Image

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Parsing command output into variable

#5 Post by ChickenSoup » 17 Jan 2011 10:42

Did you see my modification to the the for loop piping it to a find? Does that do it?

FOR /F "tokens=2 delims=()" %%a IN ('"%~dp0devcon.exe find =ports *VID_10C4^&PID_EA60*" ^| find "COM"') DO set comnumber=%%a

l95theses
Posts: 4
Joined: 17 Jan 2011 08:14

Re: Parsing command output into variable

#6 Post by l95theses » 17 Jan 2011 11:32

that just results in: "ECHO is off."

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

Re: Parsing command output into variable

#7 Post by aGerman » 17 Jan 2011 12:12

Hmm... Try:

Code: Select all

@echo off &setlocal
for /f "tokens=2 delims=()" %%a in ('"%~dp0devcon.exe find =ports *VID_10C4^&PID_EA60*"') do (
  if not defined comnumber set "comnumber=%%a"
)
echo %comnumber%
pause

Regards
aGerman

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Parsing command output into variable

#8 Post by avery_larry » 17 Jan 2011 12:18

It looks like the devcon output has 3 lines, so the for loop goes through 3 times. The first line doesn't have () so it skips the "do" portion. The next 2 lines both have (), so the "set" command is run twice -- first it sets the variable to com5, and then it sets it to s. The variable isn't 2 lines, it just gets set 2 separate times.

At least I think that.

Anyway -- if you only want the 1st instance to work, then you can break the for loop with a goto statement like this:

Code: Select all

FOR /F "tokens=2 delims=()" %%a IN ('"%~dp0devcon.exe find =ports *VID_10C4^&PID_EA60*" ^| find "COM"') DO set comnumber=%%a&&goto :continue
:continue
echo %comnumber%

l95theses
Posts: 4
Joined: 17 Jan 2011 08:14

Re: Parsing command output into variable

#9 Post by l95theses » 17 Jan 2011 13:07

Thank you avery_larry. You came up with a 100% fix for me.
This post can be closed if necessary.

Post Reply