How Can I Open a Text File as Unicode?

How Can I Open a Text File as Unicode?

  • Comments 8
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! I have some text files that include Unicode characters. When I try to open those files using a script all I get back is gibberish. How can I open a text file as Unicode?

-- FA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, FA. You know, the truly great magicians don’t concoct elaborate tricks that rely on trap doors, secret panels, and huge puffs of smoke to hide whatever they’re doing. Instead, magicians known that the best tricks are the simple tricks, the ones that rely on something obvious and straightforward, yet something that most people overlook. The Scripting Guys tend to work the same way. When we answer a question we do exhaustive research and then brainstorm for hours, trying to come up with a script that no one but a Scripting Guy could possibly think of. We’re kidding, right? Right. Instead, the Scripting Guys usually rely on something obvious and straightforward, yet something that most people overlook.

It’s not as flashy, but it’s a heck of a lot easier.

Take your question, for example. Let’s say you have a very simple Unicode file, one that consists of the following characters:

abc ėěĩ def

What happens when you use a script to open this file and then echo back the contents? Well, you get back something like this:

Hey, Scripting Guy!


Interesting. If you remove all the ASCII characters from the file - leaving just ėěĩ - you get this when you echo back the contents:

Hey, Scripting Guy!


That’s even cooler looking. Man, we could do this all day!

But, as our manager just pointed out, what we really could (should) be doing all day is finishing this column. Fair enough; with that in mind here’s a script that will open the Unicode file and correctly echo back the contents:

Const ForReading = 1
Const TriStateTrue = -1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("c:\scripts\test.txt", ForReading,False,TriStateTrue)

strText = objFile.ReadAll
objFile.Close

Wscript.Echo strText

We start out by defining a pair of constants: ForReading and TriStateTrue. ForReading is used to open our text file for reading; TriStateTrue is used for - well, we don’t want to give away the exciting ending just yet. You’ll find out in a moment what TriStateTrue (with a value of -1) is used for.

After creating an instance of the FileSystemObject we then call the OpenTextFile method to open the file C:\Scripts\Test.txt. What do you mean “boring?” Take a look at the parameters we pass to OpenTextFile:

Set objFile = objFSO.OpenTextFile("c:\scripts\test.txt", ForReading,False,TriStateTrue)

The first two parameters probably don’t faze you much: they’re simply the full path to the file we want to open and the constant ForReading. And you’re right: this is standard operating procedure when it comes to reading text files. But what about those other two parameters, one False, the other the constant TriStateTrue?

This is where the Scripting Guys perform their magic. The optional third parameter is a Boolean parameter that, if True, creates the specified text file if the file cannot be found. Because we’re only interested in opening an existing file, we set this parameter to False (which is also the default value).

That brings us to parameter 4, our old pal TriStateTrue. If the fourth parameter passed to the OpenTextFile method is equal to -1 then the file will be opened as a Unicode file. It’s that easy. Leave the fourth parameter off and the file will be opened as ASCII; set the fourth parameter to -1 and - presto-changeo! - your file will be opened as Unicode,

Thank you, thank you; you’re too kind. How about a hand for our lovely assistant, Marjorie?

What’s that? Of course it works. Take a look at the message box we generate:

Hey, Scripting Guy!


For our next trick, we will now make one of the Scripting Guys disappear and reappear at his son’s high school baseball game. (Did we mention that the Scripting Kid is batting over .400 three-fourths of the way through the season? Never mind: we don’t want bore you with a Scripting Dad bragging about his son. - Editor’s Note: Only the other Scripting Guys get that pleasure.) See you tomorrow.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Scripting Guy,

    first, I want to thank you very much for your answer to FA's question!

    It helped me with similar problem: crashing on Write method when writing unicode text to the file.

    But I would like to kindly suggest that your answer would be even better if it was shorter. ;-)

    E.g. something like this: "Use '-1' value in place of fourth parameter in OpenTextFile() method."

    Many people need to solve the problem fast and reading long prologs and broad comments is not what they can afford.

    And also, some of the readers (as me, for example) are not native speakers and thus it may be even harder to find quickly the core of the text.

    However, thanks again for helping me with my problem. :)

  • thank you for the solution, and

    Agree with ja_ro  ;-)

  • Thanks for the solution.

    Ppl should be more grateful, jeez!

  • I enjoyed the humanity in the post.

  • Not sure whether this is related but I am currently battling with a log file that appears fine in Notepad and when typed to the console, but when I open it in Edit (remember that?) or read the file in using OpenTextFile, it appears with spaces between every letter.  Is this Unicode?  More importantly, is there a way that I can get this file reading like a 'normal' text file?

  • object.OpenTextFile(filename[, iomode[, create[, format]]])

    format = -1 for unicode.

  • Thanks a lot. It had solved my problem.

    Regards,

    Rohit

    Xpode.com

  • Hello Scripting Guys!

    Awesome article. It helped me out. Also, please bear in mind, that TristateTrue is NOT equivalent to True (this made me feel PITA for a while...).

    ja_ro has some right, I only would say that to satisfy him, you could include an openable section named "Quick solution", where the single sentence with the answer could be find.

    Anyway, another question: when I open a file as UNICODE (just like described here) and the file is actually NOT Unicode, I get question marks instead of the data. Can it be checked somehow?

    Thanks!