Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 :(

do (
set var=%%a
goto :done
)
:done
echo %var%
=====================================
Helping others achieve escape felicityM2

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

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!

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

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

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

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |