0

I have a file context.xml which contains following..

<import resource="beans.xml"/>

I need to comment this line as follows using batch file.. Please help.. Thanks in advance..

<!--import resource="beans.xml"/-->

I tried this.. But its not working..

@echo off 
setlocal enableextensions enabledelayedexpansion

set "search= <import resource="beans.xml"/> "
set "replace= <!--import resource="beans.xml"/--> "

set "textFile=context.xml"

for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    >>"%textFile%" echo(!line!
    endlocal
)
Allabakash
  • 11
  • 1
  • Does this processing need to be XML aware? E.g. does it also need to find e.g. `` where `a` is set to the same default namespace that is in effect for your given example? Could `import` have content, and how should that be dealt with? If your answers are that it does need to be XML aware, you should be looking to use a proper XML processor for this. – Damien_The_Unbeliever Jul 24 '15 at 09:20

1 Answers1

0

check the replacer.bat :

call replacer.bat e?content.xml "<import resource=\u0022beans.xml\u0022/>" "<!--import resource=\u0022beans.xml\u0022/-->"

as you have quotes in the strings you want to replace you need e? before the file name which will evaluate the special symbols.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Thanks for your answer. But think if there are multiple import statements in xml file as follows.. .. I want to comment only beans.xml line.. how can i do that? – Allabakash Jul 24 '15 at 10:11
  • @Allabakash - have you tested this? it will replace only `` everywhere it is found. `beans44` and `beans111` wont be touched. – npocmaka Jul 24 '15 at 10:18