Page 1 of 1

Insert Text in Second Last Line of .xml

Posted: 25 Jan 2022 21:54
by STAiNLESS
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.

Re: Insert Text in Second Last Line of .xml

Posted: 26 Jan 2022 08:25
by Squashman
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.

Re: Insert Text in Second Last Line of .xml

Posted: 28 Jan 2022 18:13
by STAiNLESS
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

Re: Insert Text in Second Last Line of .xml

Posted: 29 Jan 2022 16:47
by Eureka!
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

Posted: 30 Jan 2022 04:33
by STAiNLESS
It sure did, thanks so much :)

Re: Insert Text in Second Last Line of .xml

Posted: 30 Jan 2022 10:18
by Squashman
STAiNLESS wrote:
28 Jan 2022 18:13
To me it does not seem to be escaping the / in </menu>, therefore its excluding any line with menu in it?
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

Posted: 30 Jan 2022 16:27
by STAiNLESS
Thanks to both of you, I got what I needed.
So thanks :)