Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#1
Post
by renzlo » 24 Jun 2011 20:19
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?
-
scienceguru1.bat
- Posts: 44
- Joined: 01 Jan 2011 20:54
#2
Post
by scienceguru1.bat » 24 Jun 2011 20:40
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~
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#3
Post
by renzlo » 24 Jun 2011 20:43
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.
-
scienceguru1.bat
- Posts: 44
- Joined: 01 Jan 2011 20:54
#4
Post
by scienceguru1.bat » 24 Jun 2011 20:50
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.
-
Acy Forsythe
- Posts: 126
- Joined: 10 Jun 2011 10:30
#6
Post
by Acy Forsythe » 24 Jun 2011 21:36
It's doable, but would you want to stop the first time you find it or keep going?
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#7
Post
by renzlo » 24 Jun 2011 21:51
it will keep going til the end of the file.
-
Acy Forsythe
- Posts: 126
- Joined: 10 Jun 2011 10:30
#8
Post
by Acy Forsythe » 24 Jun 2011 22:18
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
-
renzlo
- Posts: 116
- Joined: 03 May 2011 19:06
#9
Post
by renzlo » 24 Jun 2011 22:26
thanks Acy for the code. Working great.