Computing.Net > Forums > Programming > First line of output to variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

First line of output to variable

Reply to Message Icon

Name: Geore1999
Date: October 9, 2009 at 11:18:36 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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 :(



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 9, 2009 at 11:34:09 Pacific
Reply:

do (
set var=%%a
goto :done
)
:done
echo %var%


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

M2


0

Response Number 2
Name: Geore1999
Date: October 9, 2009 at 20:13:41 Pacific
Reply:


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


0

Response Number 3
Name: Geore1999
Date: October 9, 2009 at 20:23:05 Pacific
Reply:

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!


0

Response Number 4
Name: lee123abc
Date: October 10, 2009 at 02:07:12 Pacific
Reply:

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


0

Response Number 5
Name: Judago
Date: October 10, 2009 at 02:47:49 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: lee123abc
Date: October 10, 2009 at 06:04:18 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: First line of output to variable

Delete first line of text www.computing.net/answers/programming/delete-first-line-of-text/13312.html

Extract First Line of Text www.computing.net/answers/programming/extract-first-line-of-text/13939.html

Delete First Line of Text www.computing.net/answers/programming/delete-first-line-of-text/13940.html