Page 1 of 1
is this doable?
Posted: 24 Jun 2011 20:19
by renzlo
for example:
test.xml
Code: Select all
<Supplier>Random strings</Supplier>
<Number />
<Description />
<Unit />
<Amount />
<Table>
<Number />
<Description />
<Unit />
<Amount />
findstr "<Supplier>" test.xml and echo the next line, so the output is:
anyone?
Re: is this doable?
Posted: 24 Jun 2011 20:40
by scienceguru1.bat
can you refrase that? i know a bit about xml and that is confusing. could you correct it or add more around it so that it makes sense?
thanks
~C~
Re: is this doable?
Posted: 24 Jun 2011 20:43
by renzlo
to simplify, I just want to find a specific string and when found it will echo the line after the found line of the found string.
Re: is this doable?
Posted: 24 Jun 2011 20:50
by scienceguru1.bat
i ment the xml, but whatever. i would quess not, because findstr (from what i've seen) tends to be extracting data from a variable, not neccessarely a xml tag. agerman or another expert would probably know more about that.
Re: is this doable?
Posted: 24 Jun 2011 20:56
by renzlo
i hope it is doable
Re: is this doable?
Posted: 24 Jun 2011 21:36
by Acy Forsythe
It's doable, but would you want to stop the first time you find it or keep going?
Re: is this doable?
Posted: 24 Jun 2011 21:51
by renzlo
it will keep going til the end of the file.
Re: is this doable?
Posted: 24 Jun 2011 22:18
by Acy Forsythe
Someone else can probably improve on this, but this does the trick:
Parser.batCode: Select all
@Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: You can change Set SrchString=Supplier to Set SrchString=%1
:: then run the batch file like this: Parser.bat Supplier
:: Or you can just edit the batch file every time you need to change the search string
:: Same with the Filename set, %1 is parameter 1, %2 is parameter two.
::
::Set Filename=%1
::Set SrchString=%2
::
:: If you replaced the SET commands with the 2 above, you would run it like this:
:: Parser.bat test.xml Supplier
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Set Counter=1
Set SrchString=Supplier
Set Filename=test.xml
FOR /F "tokens=1 delims=^<^>" %%a IN (%Filename%) DO (
IF %%a==%SrchString% Call :Skip !Counter!
Set /a Counter+=%Counter%
)
exit /b
:skip
FOR /F "tokens=* skip=%~1 delims=" %%b IN (%Filename%) DO (
echo %%b
exit /b
)
ENDLOCAL
exit /b
Re: is this doable?
Posted: 24 Jun 2011 22:26
by renzlo
thanks Acy for the code. Working great.
Re: is this doable?
Posted: 25 Jun 2011 09:54
by Acy Forsythe
You're welcome.