How Can I Open a Text File as Unicode?


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.


Discussion is closed.

Feedback usabilla icon