I have an xml file with <menu> </menu> opening\closing tags.
I want to add lines of text before the last line (the closing </menu> tag).
example:
<?xml version="1.0"?>
<menu>
<header>
<listname>PC Games</listname>
<lastlistupdate>10/09/2021</lastlistupdate>
<listversion>0.1</listversion>
</header>
<game name="Streets of Rage 4" index="true" image="0">
<description>Streets of Rage 4</description>
<manufacturer>Dotemu</manufacturer>
<year>2020</year>
<genre>Beat-'Em-Up</genre>
<score>0.85</score>
<players>4</players>
<enabled>Yes</enabled>
</game>
</menu>
I want to add more games (all the info between the <game> tags), I can handle the echo >> part, i just dont know how to echo it above the </menu> line.
Any help is appreciated thanks.
Insert Text in Second Last Line of .xml
Moderator: DosItHelp
Re: Insert Text in Second Last Line of .xml
If </menu> is the last line of the xml file it should be pretty straight forward. You have to make a new file so just redirect all the lines currently in the file to a new file except for </menu> with the find or findstr commands.
Or when you are reading the file with the FOR /F command, check to see if the line equals </menu> with the the IF command. If TRUE, echo out all your new games and then echo the last line.
Or when you are reading the file with the FOR /F command, check to see if the line equals </menu> with the the IF command. If TRUE, echo out all your new games and then echo the last line.
Re: Insert Text in Second Last Line of .xml
Thanks for that reply, I am trying your first suggestion except failing as it also leaves out the opening <menu> tag.
This is what I have gotten it down to so far
for /F "tokens=*" %%i in ('type "PC Games.xml" ^|find /I /V "<^/menu>"') do (echo %%i>> temp.xml)
To me it does not seem to be escaping the / in </menu>, therefore its excluding any line with menu in it?
just to refresh,
opening tag is <menu>
closing tag is </menu>
only the closing tag is not to be copied to the temp.xml
This is what I have gotten it down to so far
for /F "tokens=*" %%i in ('type "PC Games.xml" ^|find /I /V "<^/menu>"') do (echo %%i>> temp.xml)
To me it does not seem to be escaping the / in </menu>, therefore its excluding any line with menu in it?
just to refresh,
opening tag is <menu>
closing tag is </menu>
only the closing tag is not to be copied to the temp.xml
Re: Insert Text in Second Last Line of .xml
Does this work for you?
Code: Select all
findstr /v /i /c:"</menu>" "PC Games.xml" > temp.xml
Re: Insert Text in Second Last Line of .xml
It sure did, thanks so much
Re: Insert Text in Second Last Line of .xml
What made you think you needed to escape anything in the search string using the find command? The help syntax does not reference it at all. If you read the help for the findstr command then in certain instances you do need to escape certain characters
Re: Insert Text in Second Last Line of .xml
Thanks to both of you, I got what I needed.
So thanks
So thanks