3
Dim txtFile, fileObj, streamObj, s

Set txtFile = CreateObject(fileName)

Set streamObj = CreatreObject("adodb.Stream")
streamObj.Charset = "UTF-8"
streamObj.open
Set fileObj = txtFile.OpenTextFile("filePath")

Do Until fileObj.AtEndOfStream
      s = fileObj.ReadLine
      txtObj.WriteText s
Loop

txtObj.SaveToFile "D:\A4\Message_tool\surya.msg", 2
fileObj.Close

After the execution this code the encoding format of surya.msg is "ANSCII" but I want it to be "UTF-8"

surya4969
  • 127
  • 2
  • 12

2 Answers2

2
Const adTypeText = 2
Const adSaveCreateOverWrite = 2

Dim inputFile, outputFile
    inputFile = "input_file.txt"
    outputFile = "output_file.txt"

Dim inputStream
    Set inputStream  = WScript.CreateObject("adodb.stream")
    With inputStream
        .Type = adTypeText
        .Charset = "unicode"
        .Open
        .LoadFromFile inputFile
    End With

Dim outputStream
    Set outputStream = WScript.CreateObject("adodb.stream")
    With outputStream
        .Type = adTypeText
        .Charset = "utf-8"
        .Open
        .WriteText inputStream.ReadText
        .SaveToFile outputFile, adSaveCreateOverWrite
    End With

    inputStream.Close
    outputStream.Close
MC ND
  • 65,671
  • 6
  • 67
  • 106
0

Unicode-encoded text files can be opened in the usual way using the OpenTextFile() method of the FileSystemObject class. Just pass True/-1 for the 4th ("format") parameter.

strText = objFSO.OpenTextFile(inputFile, , , True).ReadAll()

To encode your text as UTF-8, you'll need to use the ADO Stream class.

Const adTypeText = 2
Const adSaveCreateOverWrite = 2

With CreateObject("ADODB.Stream")
    .Type = adTypeText
    .Charset = "utf-8"
    .Open
    .WriteText strText
    .SaveToFile "D:\A4\Message_tool\surya.msg", adSaveCreateOverWrite 
End With

For a list of character encodings supported by Windows, look within the following registry key:

HKEY_CLASSES_ROOT\MIME\Database\Charset
Bond
  • 15,435
  • 6
  • 28
  • 53
  • The fourth/format parameter of the .OpenTextFile method takes a TriState, not a Boolean value. – Ekkehard.Horner Sep 05 '14 at 18:09
  • @Ekkehard.Horner Yep. But you can just use `True` for Unicode or `False` for ASCII to achieve the same effect. – Bond Sep 05 '14 at 18:21
  • @Bond - maybe you *can*, but you shouldn't. Furthermore you shouldn't insist on bad practices againt better knowledge: -1. – Ekkehard.Horner Sep 05 '14 at 18:35
  • @Ekkehard.Horner LOL, whatever. Programmers having been taking advantage of `True = -1` and `False = 0` for longer than I've been around. I guess everyone who ever wrote a `while(1)` loop in C++ was "insisting on bad practices". – Bond Sep 05 '14 at 18:47
  • @Bond - Your guess is correct, because an endless loop in C++ is to be expressed as `while(true)` or `for(;;)`. `while(1)` is good C89/90, but made obsolete by the introduction of a boolean datatype in the C99 standard. – Ekkehard.Horner Sep 05 '14 at 20:20
  • @Ekkehard.Horner And some of the best programmers still use the technique [even today](http://stackoverflow.com/questions/2669690/why-does-google-prepend-while1-to-their-json-responses). – Bond Sep 05 '14 at 20:25