First line of output to variable

October 9, 2009 at 11:18:36
Specs: Windows XP
Hello all,

Can someone help me read the first line of output from findstr to a variable?

This is what i have so far:

for /f "tokens=1 delims=" %%a in ('findstr "<text>" file.xml') do @set var=%%a
but that just adds the last line of the output from findstr, not the first.


Basically the file is a standard .xml rss feed, and I only wish to set the first instance of "<text>variable</text>" as my variable.

<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<text>1234567</text>
<geo_enabled>false</geo_enabled>
<verified>false</verified>
<text>something else</text>
</status>

I really don't care if my variable has the <text> tags in it either: 1234567 or text>1234567</text> is AOK!

THANK YOU! I have been at this for hours now :(


See More: First line of output to variable

Report •


#1
October 9, 2009 at 11:34:09
do (
set var=%%a
goto :done
)
:done
echo %var%


=====================================
Helping others achieve escape felicity

M2


Report •

#2
October 9, 2009 at 20:13:41

for /f "tokens=1 delims= " %%a in ('findstr "<text>" update.xml') do @set var=%%a && goto DONE
:done
ECHO Thanks Mechanix2Go!

Report •

#3
October 9, 2009 at 20:23:05
Also,

This got rid of the <text> tags in the variable:

for /f "tokens=1 delims= " %%a in ('findstr "<text>" update.xml') do @set var=%%a && goto DONE
:DONE
set var=%var:~+6%

Thanks again!


Report •

Related Solutions

#4
October 10, 2009 at 02:07:12
I was under the impression you couldn't use GOTO :COMMANDS in a for loop. M2, please explain what is happening with this for loop! I just dont see why I couldn't in my last code but it can be done here.

Thanks


Report •

#5
October 10, 2009 at 02:47:49
I'm here so I may as well explain it.........

Goto breaks the iterating nature of the for loop. Once used the loop will no longer be executed, which is exactly what is wanted above. In cases that you want the loop to continue executing goto can't be used. I don't recommend using goto in a for /l loop because it seem to keep iterating although the commands aren't executed.

echo on
for /l %%a in ( 1 1 10) do goto break
:break
pause


Batch Variable how to


Report •

#6
October 10, 2009 at 06:04:18
Thanks Judago. This is what I was trying to achieve with my code the other day, I will take a re-visit to that batch file for interest reasons.

Cheers


Report •


Ask Question