Problem finding and replacing a batch file

I have an XML file, and I have a batch file to search for a specific line in this file, replace it with a user-defined line, and then output it to a new XML file:

@echo off > entities_1.xml setLocal EnableDelayedExpansion if exist entities_1.xml del entities_1.xml set /p name= What is the new space NAME? for /f "tokens=* delims= " %%G in (entities.xml) do ( set str=%%G set str=!str:[Test Space]=[%name%]! echo !str! >> entities_1.xml ) 

This works, and all instances of [Test Space] are replaced with a user-defined value.

My problem, however, is that the batch file also deletes instances of exclamation points (!). So, for example, in XML there are lines similar to this:

 <property name="title"><![CDATA[TEST2]]></property> 

When the script package is launched, it replaces the above:

 <property name="title"><[CDATA[TEST2]]></property> 

those. separating !.

Where am I mistaken? Any ideas?

+2
batch-file
Dec 02 '10 at 10:11
source share
3 answers

This is the wrong way to get a string using set str = %% G when slow extension is enabled.
This is due to the fact that the delayed expansion phase is located after the expansion phase %% v.

With the extension delay turned off, you have a problem that you cannot replace the string in a safe way. Therefore, you need to switch the delayed extension.

 @echo off > entities_1.xml setLocal DisableDelayedExpansion if exist entities_1.xml del entities_1.xml set /p name= What is the new space NAME? for /f "tokens=* delims= " %%G in (entities.xml) do ( set str=%%G setLocal EnableDelayedExpansion set str=!str:[Test Space]=[%name%]! >> entities_1.xml echo(!str! endlocal ) 

Redirection switching, so you did not always add space.
Using echo ( , you will not get echoed on if the string is empty !!

+5
Dec 02 '10 at 13:27
source share

You need to create an empty entities_1.xml file because for some unclear reason >> it will not create it using cmd as a wrapper.
Therefore, use TYPE NUL > entities_1.xml immediately before your FOR loop to create a file that is 0 bytes long.

+2
Dec 05 '10 at
source share

check my answer to a question about the same topic, he answered perfectly.

How to replace a line on the second line in a text file using a batch file?

hope this helps!

0
Dec 05 '10 at 22:31
source share



All Articles