Page 1 of 1

Parsing command output into variable

Posted: 17 Jan 2011 08:25
by l95theses
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!

Re: Parsing command output into variable

Posted: 17 Jan 2011 09:34
by aGerman
What about delims=() :?:

Regards
aGerman

Re: Parsing command output into variable

Posted: 17 Jan 2011 09:39
by ChickenSoup
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

Re: Parsing command output into variable

Posted: 17 Jan 2011 09:57
by l95theses
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

Re: Parsing command output into variable

Posted: 17 Jan 2011 10:42
by ChickenSoup
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

Re: Parsing command output into variable

Posted: 17 Jan 2011 11:32
by l95theses
that just results in: "ECHO is off."

Re: Parsing command output into variable

Posted: 17 Jan 2011 12:12
by aGerman
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

Re: Parsing command output into variable

Posted: 17 Jan 2011 12:18
by avery_larry
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%

Re: Parsing command output into variable

Posted: 17 Jan 2011 13:07
by l95theses
Thank you avery_larry. You came up with a 100% fix for me.
This post can be closed if necessary.