-1

Can any one help me by suggesting a batch script to find 'utf-16' string in multiple xml files in a directory and replace it with 'utf-8'. With out using find and replace tools. The replacement need to be done in existing file itself.

  • 1
  • @DavidPostill did you not see the words utf-16 and utf-8 The question may or may not be a bit confused but you are not addressing that by just pointing to that other question – barlop Mar 10 '15 at 9:24
  • if you have a file that has a mix of utf-8 and unnecessarily utf-16 then is your file a mixed up file format, are you sure you have a file like that? – barlop Mar 10 '15 at 9:27
  • @barlop I read it as replace the literal string utf-16 and replace with utf-8. If the OP wants to change encoding I don't think thats possible from cmd. – DavidPostill Mar 10 '15 at 9:29
  • 1
    @barlop OP asked for a batch script ( With out using find and replace tools). He didn't ask how to use external tools to do the conversion. AFAIK it cannot be done with just a batch script without using external tools. In any case it is still unclear what the OP is trying to do. Please don't insult me using "pontificate" whan the question is unclear ... – DavidPostill Mar 10 '15 at 11:28
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)}
| improve this answer | |
  • I guess all that code in zero.xsl is to get it to do it for many files? what code would do it for just one file? that's a lot of code just to change a literal string from one thing to another. (if that's what he wants to do) – barlop Mar 10 '15 at 9:47
  • @barlop zero.xsl shows the transformation that does nothing with the text. Do the same for multiple files, you just need to loop FOR /R %%i IN (*.xml) DO . Which lines you want to replace in the xml file, and where? – STTR Mar 10 '15 at 9:51
  • so all that code in zero.xsl is just to replace one string with another string? – barlop Mar 10 '15 at 10:37
  • @barlop No, it's an empty conversion. When the file is converted into itself but for example with UTF-8 encoding specified in the options. If you have indicated in the example exactly what you want to convert, you can write a specific string conversion. Because it will be different for elements and attributes for the complete line for the substring. – STTR Mar 10 '15 at 10:46
  • thanks, you should clarify that all that does nothing and you specify the conversion, you should even specify an example of it converting a specific string. Then it'll be more useful for people. Yor answer should be useful for people looking for the answer to the question, other than just the OP. interesting thing re powershell too you mention there. hopefully the OP will cliarify what he wants to convert. But in the meantime, or even regardless, give a more useful example (one that works, that others can use and adapt) so that your example isn't a completely empty shell. – barlop Mar 10 '15 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.