Problem with search and replace batch file -


i have xml file , have batch file search specific string within file, replace string defined user , output new xml file:

@echo off > entities_1.xml setlocal enabledelayedexpansion  if exist entities_1.xml del entities_1.xml set /p name= new space name?  /f "tokens=* delims= " %%g in (entities.xml) ( set str=%%g set str=!str:[test space]=[%name%]! echo !str! >> entities_1.xml ) 

this works , instances of [test space] replaced value defined user.

my issue, however, batch file stripping out instances of exclamation (!) marks. example in xml there lines similar this:

<property name="title"><![cdata[test2]]></property> 

when batch script run, replacing above with:

<property name="title"><[cdata[test2]]></property> 

i.e. stripping out !.

where going wrong? ideas?

it's wrong way of receiving line set str=%%g while delayed expansion enabled.
it's because delayed expansion phase after %%v expansion phase.

with disabled delayed expansion got problem, can not replace string in safe way. have toggle delayed expansion.

@echo off > entities_1.xml setlocal disabledelayedexpansion  if exist entities_1.xml del entities_1.xml set /p name= new space name?  /f "tokens=* delims= " %%g in (entities.xml) (     set str=%%g     setlocal enabledelayedexpansion     set str=!str:[test space]=[%name%]!     >> entities_1.xml echo(!str!     endlocal ) 

switching redirection, didn't append space.
using echo(, not echo on if !str! empty.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -