33

I have a text file saved as UTF-8 and when I try to read the file it gives me weird characters and not the correct characters (it contains Chinese characters). How can I make it give me the correct Chinese characters?

Option Explicit

Dim objFSO, strTextFile, strData, strLine, arrLines, aniTextFile, aniData, aniLines, aniLine, objTextFile, fso, inputFileList, listFile, fname
Dim iim1, iret, iret2, iret3, i
Const ForReading   = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
strTextFile = "C:\Users\admin\Desktop\ArtistCG\folder.txt"
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
arrLines = Split(strData,vbCrLf)

aniTextFile = "C:\Users\admin\Desktop\ArtistCG\folder-list.txt"
aniData = objFSO.OpenTextFile(aniTextFile,ForReading).ReadAll
aniLines = Split(aniData,vbCrLf)

For i = 0 To UBound(arrLines)
  strData = objFSO.OpenTextFile(arrLines(i),ForReading).ReadAll
  WScript.Echo strData

  Set listFile = objFSO.OpenTextFile(aniLines(i),ForReading)
  Do While Not listFile.AtEndOfStream
    fName = listFile.ReadLine
    WScript.Echo fName
  Loop
  listFile.Close
Next 
Trish Pham
  • 439
  • 2
  • 6
  • 13

2 Answers2

63

From the documentation:

The FSO can read only ASCII text files. You cannot use the FSO to read Unicode files or to read binary file formats such as Microsoft Word or Microsoft Excel.

Since you got weird characters, I guess that's somewhat incorrect and the file was read in some 8-bit windows code page because if it really could read only ASCII, you would have seen ????

Anyway, if you can use ADO, you can do this:

Dim objStream, strData

Set objStream = CreateObject("ADODB.Stream")

objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile("C:\Users\admin\Desktop\ArtistCG\folder.txt")

strData = objStream.ReadText()

objStream.Close
Set objStream = Nothing
Roger Barreto
  • 2,004
  • 1
  • 17
  • 21
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • 2
    lol, it's true that FSO can't read UTF-8, but the MSDN docs makes me laugh about Unicode and binary files. – Panayot Karabakalov Mar 16 '13 at 00:41
  • 3
    Don't forget to `objStream.Close` after `objStream.ReadText()`. – Olexa Apr 02 '14 at 11:59
  • 2
    It's true that `FSO` can't read UTF-8, but actually you can open the file as ASCII with `FSO` and [read the content char by char in loop](http://stackoverflow.com/a/6087783/2165759), then convert that sequence of chars to the string [considering each char as a byte of UTF-8 encoded file](https://en.wikipedia.org/wiki/UTF-8#Description). It's just an academic interest; `ADODB.Stream` definitely is the most efficient. – omegastripes Mar 03 '16 at 22:25
  • 2
    It's not true that "FSO can read only ASCII text files" - FileSystemObject can read UTF-16 files, but not UTF-8 see @Ekkehard.Horner comment on the answer by user2171987 – Daz Dec 02 '19 at 14:46
  • objStream.ReadText() doesn work – fsalazar_sch Jul 13 '22 at 05:14
-6

You can read UTF 8 formatted files by using the , True when with the file system object.

sFile = "C:\Users\admin\Desktop\ArtistCG\folder.txt"
Set FS = CreateObject("scripting.filesystemobject")
Set oReadfile = FS.OpenTextFile(sFile, 1, False, True)
TextFromFile = sReadfile.ReadAll
  • 14
    -0.99 The *tristate* fourth/format parameter to .OpenTextFile() allows to open a text file using system's default (-2), UTF-16 (-1), or 'ASCII' (0) encoding, but *not* UTF-8. – Ekkehard.Horner Mar 15 '13 at 06:09
  • 2
    This comment helped me to finally figure how to read a UTF-16 LE ini file I've been trying to modify.I could save the file as UTF-16 LE when writing but need to add the fourth parameter as -1 (TristateTrue) for the read function to correctly find the setting to be modified. Thanks – Supernatix Jan 09 '19 at 14:13