How to search and replace a string in multiple xml files (within a directory) with Windows CMD

Solution 1:

Use any XSLT-processor. For example msxsl.

Command Line Transformation Utility

MSXML 4.0 Service Pack 2 (Microsoft XML Core Services)

zero.xsl - style sheet transform test.xml to test2.xml

<xsl:output method="xml" encoding="UTF-8" /> convert xml to UTF-8.

Zeroxml test.xml

Zeroxml.cmd:

@echo off
@set name=%1
msxsl.exe %name% zero.xsl -o %name:~0,-4%2.xml

zero.xsl:

<!-- The Identity Transformation -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- <xsl:output method="html" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/> -->


<xsl:output method="html" media-type="application/vnd.ms-excel" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

<!-- <xsl:output omit-xml-declaration="no" indent="yes" encoding="UTF-8" method="html" />  -->
<!-- <xsl:output method="xml" media-type="application/vnd.ms-excel" encoding="UTF-8" indent="yes"     omit-xml-declaration="no"/>  -->
<!-- <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>  -->
<!-- <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> -->

  <!-- Whenever you match any node or any attribute -->
  <xsl:template match="node()|@*">
    <!-- Copy the current node -->
    <xsl:copy>
      <!-- Including any attributes it has and any child nodes -->
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

 <xsl:template match="*|@*|comment()|
                          processing-instruction()|text()">
         <xsl:copy>
             <xsl:apply-templates select="*|@*|comment()|
                                      processing-instruction()|text()"/>
         </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Way2, convert UTF-16 to UTF-8 command line:

Unicode - UTF-16 format little-endian byte order.

powershell gc test.xml -encoding Unicode^|sc testUTF8.xml -encoding UTF8

BigEndianUnicode - UTF-16 format big-endian byte order.

powershell gc test.xml -encoding BigEndianUnicode^|sc testUTF8.xml -encoding UTF8

Convert all xml file UTF-16 in SourceDirXML directory and subdirectory to UTF-8

powershell $in='C:\SourceDirXML';$out='C:\OutputUTF8XML\';ls -Fo -r $in -Fi *.xml^|%{(gc $_.FullName -encoding Unicode^|sc ($out+$_.Name) -encoding UTF8)}

Search and replace a string in multiple xml files (within a directory) with Windows CMD:

powershell $in='C:\SourceDirXML';$out='C:\OutputUTF8XML\';ls -Fo -r $in -Fi *.xml^|%{(gc $_.FullName^|%{$_ -replace 'oldstring','newstring'}^|sc ($out+$_.Name) -encoding UTF8)}