Insert Text in Second Last Line of .xml

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
STAiNLESS
Posts: 4
Joined: 25 Jan 2022 21:38

Insert Text in Second Last Line of .xml

#1 Post by STAiNLESS » 25 Jan 2022 21:54

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Insert Text in Second Last Line of .xml

#2 Post by Squashman » 26 Jan 2022 08:25

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.

STAiNLESS
Posts: 4
Joined: 25 Jan 2022 21:38

Re: Insert Text in Second Last Line of .xml

#3 Post by STAiNLESS » 28 Jan 2022 18:13

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

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Insert Text in Second Last Line of .xml

#4 Post by Eureka! » 29 Jan 2022 16:47

Does this work for you?

Code: Select all

findstr /v /i /c:"</menu>" "PC Games.xml" > temp.xml

STAiNLESS
Posts: 4
Joined: 25 Jan 2022 21:38

Re: Insert Text in Second Last Line of .xml

#5 Post by STAiNLESS » 30 Jan 2022 04:33

It sure did, thanks so much :)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Insert Text in Second Last Line of .xml

#6 Post by Squashman » 30 Jan 2022 10:18

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

STAiNLESS
Posts: 4
Joined: 25 Jan 2022 21:38

Re: Insert Text in Second Last Line of .xml

#7 Post by STAiNLESS » 30 Jan 2022 16:27

Thanks to both of you, I got what I needed.
So thanks :)

Post Reply