Replace text in XML using batch file

Microsoft Windows xp professional w/serv...
January 21, 2010 at 10:42:49
Specs: Windows XP
I have put this question in the wrong forum. re-posting it here:
I am trying to change the project version in a xml file
using dos. when i execute the following code, an empty new.xml is created..there is nothing on the stdout as well. plz help

------
setLocal EnableDelayedExpansion > new.xml

for /f "tokens=* delims= " %%a in (%1) do (
echo %%a
if %%a equ "^<currentVersion^>3.0.0^</currentVersion^>" (
echo ^<currentVersion^>3.0.0.1^</currentVersion^> >>new.xml
) else (
echo %%a
echo %%a>> new.xml
)


See More: Replace text in XML using batch file

Report •


#1
January 21, 2010 at 10:51:24
i think you need to add one more ) at the very bottom. as it is, they're unbalanced:
)

Report •

#2
January 21, 2010 at 11:02:05
thanks nbrane. that was stupid of me. i looked at that piece 100 times. now atleast the code is running..

but it isnt doing what its supposed to do. here is the part from my project.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<project>

<extend>${maven.abcd.services.project.xml}</extend>

<id>xyz</id>
<groupId>xyz</groupId>
<name>xyz</name>
<currentVersion>3.0.0</currentVersion>

<inceptionYear>2008</inceptionYear>
<package>com.pq.abcd.xyz</package>
<description>A service for event management.</description>

the <currentVersion> tag data didn't change. it still remained 3.0.0. i need to be changed to 3.0.0.1

also i see 'ECHO is off' is added to the new.xml if there are two empty lines in project.xml. can we fix that

</dependency>
ECHO is off.
<dependency>


Report •

#3
January 21, 2010 at 11:29:52
put quotes around %%a at the "if":
if "%%a" equ "^<currentVersion^>3.0.0^</currentVersion^>"
and make echos into echodots:
echo.%%a>>newfile
might fix the ECHO IS OFF stuff.
(i found out the hard way: echo xyz >>file
the space between the xyz and the >> also gets echoed!
sometimes you want that, sometimes not)
also, null lines in the inputfile will be passed over completely by batch, so if you want/need to retain them in the outputfile, let me know, but otherwise...
(because that requires a bunch more changing of script...)
ps: just in case... if your inputfile has a space in any of the lines, you might want to change this:
for /f "tokens=* delims= "
to this:
for /f "tokens=* delims="
because otherwise %%a will only capture upto the first space on the line.

Report •

Related Solutions

#4
January 21, 2010 at 11:40:14
@nbrane

echo.%%a>>newfile

It's not an issue with a for loop variable but with a standard var or text constant there can be problems when a line ends in a number, it conflicts with stream redirection:

echo test 2>file
echo look down
type file

By far the safest way is to put the redirection before the command:

>> file echo hello!


Batch Variable how to


Report •

#5
January 21, 2010 at 12:01:12
thanks nbrane and judago. the equ match is still not working..
i tried with and without escaping the <,> symbols. didn't work.

here is my latest code:

@echo off
setLocal EnableDelayedExpansion > new.xml

for /f "tokens=* delims=" %%a in (%1) do (
if "%%a" equ "<currentVersion>3.0.0</currentVersion>" (
echo matchhhhhh
>> new.xml echo ^<currentVersion^>3.0.0.1^</currentVersion^>
) else (
>> new.xml echo. %%a
)
)

the "ECHO is off" issue was cleared by using echo. (dot)

help!!


Report •

#6
January 21, 2010 at 12:18:48
Give this a try:

@echo off
rem !'s are important
setlocal disabledelayedexpansion > new.xml
for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
    set line=%%a
    setlocal enabledelayedexpansion
    if /i "!line:*]=!"=="<currentVersion>3.0.0</currentVersion>" (
        echo matchhhhhh
        >> new.xml echo ^<currentVersion^>3.0.0.1^</currentVersion^>
    ) else (
        rem delex var + substring + echo. don't like each other.
        if "!line:*]=!"=="" (
            >> new.xml echo.
        ) else (
            >> new.xml echo !line:*]=!
        )
    )
    endlocal
)
endlocal


Batch Variable how to


Report •

#7
January 21, 2010 at 13:52:14
thanks for your effort judago. but, i am sorry to say that your code didn't work either. and the 'echo is off' are back. the thing diff with your code is it reproduced even the blank lines. thats it.

here is a part of my input file, if you want to give it a shot.

<?xml version="1.0" encoding="ISO-8859-1"?>

<project>

<extend>${maven.abcd.services.project.xml}</extend>

<id>xyz</id>
<groupId>xyz</groupId>
<name>xyz</name>
<currentVersion>3.0.0</currentVersion>

<inceptionYear>2008</inceptionYear>
<package>com.pq.abcd.xyz</package>
<description>A service for management.</description>


Report •

#8
January 21, 2010 at 13:59:31
@judago, yeah, i started to suggest that to him (>>echo vs echo >>), then for some reason didn't! I ran into that little conundrum about the number> (like 6>test) and had to fix it, so i should have remembered about that too.

i wonder if this would work and if so, if it would help:
:: put quotes around it first
set test="%%a"
set test=!test:<=!
set test=!test:>=!
:: it already has the quotes, so none here!
if !test! equ "currentVersion3.0.0/currentVersion"


@zach: we crossed in the mail! sorry.
Judago will beat me to the punch, but i'll look at your code also to see if i can help

update: i pasted your code, (as it is without the escapes) and it worked:
matchhhh
etc.

i pasted your <currentversion> line into a 3-line testfile and it worked on that...
the only change i made to your code was to make the for loop only look at xyz.xml instead of *.xml, but other than that it was verbatim...


Report •

#9
January 21, 2010 at 22:02:47
I'm guessing the issue is that their are either leading
spaces or tabs and spaces or tabs on otherwise empty lines.

I am hoping this should deal with that:

@echo off
rem !'s are important
setlocal disabledelayedexpansion > new.xml
for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
    set line=%%a
    set test=%%a
    setlocal enabledelayedexpansion
    if defined test set test=!test:*]=!
    if defined test set test=!test: =!
    if defined test set test=!test:	=!
    if /i "!test:<currentVersion>3.0.0</currentVersion>=!"=="" (
        echo matchhhhhh
        set line=!line:*]=!
        >> new.xml echo !line:3.0.0=3.0.0.1!
    ) else (
        rem delex var + substring + echo. don't like each other.
        if "!test!"=="" (
            >> new.xml echo.
        ) else (
            >> new.xml echo !line:*]=!
        )
    )
    endlocal
)
endlocal


Batch Variable how to


Report •

#10
January 22, 2010 at 06:57:48
There are leading space/tabs. Sorry about that. That is my bad. nbrane and judago, you guys are unbelievable. Thank you so much for your time and effort. It works like a charm.

Report •

#11
February 23, 2010 at 04:19:54
Awsome! Thanks!!!
A lot of answers from googling says this can't be done... :))

Report •


Ask Question