Copy and edit xml file using batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
orik123
Posts: 2
Joined: 11 May 2012 04:17

Copy and edit xml file using batch

#1 Post by orik123 » 11 May 2012 04:24

Hi ppl!

I'm trying to do a very simple thing - I have an xml file and I wish to add another line to it, using a batch file.

The xml file is named: 1.xml and it looks like that:

<?xml version='1.0' encoding="UTF-8"?>
<start>hello</start>


I'm using the following code to add another line between the first line and the second:

@echo off > newfile
set /a lineNUM=0
for /f "tokens=*" %%L in (1.xml) do call :sub1 %%L
copy newfile 1.xml > nul
del newfile
goto :eof

:sub1
set /a lineNUM+=1
if %lineNUM% EQU 2 echo ^<?format="my"?^> >> newfile
echo %* >> newfile


Problem is that the xml tag is ruining this whole edit process.
Any idea how to solve this?

Thanks!
Orik.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy and edit xml file using batch

#2 Post by foxidrive » 11 May 2012 09:02

Code: Select all

@echo off
find "?xml version='1.0' encoding=" <"file.xml" >"newxmlfile.xml"
echo ^<?format="my"?^> >>"newxmlfile.xml"
more +1  <"file.xml" >>"newxmlfile.xml"

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Copy and edit xml file using batch

#3 Post by abc0502 » 11 May 2012 09:07

foxidrive wrote:

Code: Select all

@echo off
find "?xml version='1.0' encoding=" <"file.xml" >"newxmlfile.xml"
echo ^<?format="my"?^> >>"newxmlfile.xml"
more +1  <"file.xml" >>"newxmlfile.xml"


This is very smart :)

orik123
Posts: 2
Joined: 11 May 2012 04:17

Re: Copy and edit xml file using batch

#4 Post by orik123 » 12 May 2012 05:51

Foxidrive,

That's working great!
Both genius and simple as hell, why didn't I come up with that? ;)

Thanks!
Orik.

Post Reply