{"id":67503,"date":"2006-04-19T13:44:00","date_gmt":"2006-04-19T13:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/19\/how-can-i-open-a-text-file-as-unicode\/"},"modified":"2006-04-19T13:44:00","modified_gmt":"2006-04-19T13:44:00","slug":"how-can-i-open-a-text-file-as-unicode","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-open-a-text-file-as-unicode\/","title":{"rendered":"How Can I Open a Text File as Unicode?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>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?<BR><BR>&#8212; FA<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, FA. You know, the truly great magicians don\u2019t concoct elaborate tricks that rely on trap doors, secret panels, and huge puffs of smoke to hide whatever they\u2019re 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\u2019re kidding, right? Right. Instead, the Scripting Guys usually rely on something obvious and straightforward, yet something that most people overlook.<\/P>\n<P>It\u2019s not as flashy, but it\u2019s a heck of a lot easier.<\/P>\n<P>Take your question, for example. Let\u2019s say you have a very simple Unicode file, one that consists of the following characters:<\/P><PRE class=\"codeSample\">abc \u0117\u011b\u0129 def\n<\/PRE>\n<P>What happens when you use a script to open this file and then echo back the contents? Well, you get back something like this:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/unicode1.jpg\" width=\"170\" height=\"107\"> \n<P><BR>Interesting. If you remove all the ASCII characters from the file &#8211; leaving just \u0117\u011b\u0129 &#8211; you get this when you echo back the contents:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/unicode2.jpg\" width=\"170\" height=\"107\"> \n<P><BR>That\u2019s even cooler looking. Man, we could do this all day!<\/P>\n<P>But, as our manager just pointed out, what we <I>really<\/I> could (should) be doing all day is finishing this column. Fair enough; with that in mind here\u2019s a script that will open the Unicode file and correctly echo back the contents:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst TriStateTrue = -1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\test.txt&#8221;, ForReading,False,TriStateTrue)<\/p>\n<p>strText = objFile.ReadAll\nobjFile.Close<\/p>\n<p>Wscript.Echo strText\n<\/PRE>\n<P>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 &#8211; well, we don\u2019t want to give away the exciting ending just yet. You\u2019ll find out in a moment what TriStateTrue (with a value of -1) is used for.<\/P>\n<P>After creating an instance of the <B>FileSystemObject<\/B> we then call the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Test.txt. What do you mean \u201cboring?\u201d Take a look at the parameters we pass to OpenTextFile:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\test.txt&#8221;, ForReading,False,TriStateTrue)\n<\/PRE>\n<P>The first two parameters probably don\u2019t faze you much: they\u2019re simply the full path to the file we want to open and the constant ForReading. And you\u2019re right: this is standard operating procedure when it comes to reading text files. But what about those <I>other<\/I> two parameters, one <B>False<\/B>, the other the constant <B>TriStateTrue<\/B>?<\/P>\n<P>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\u2019re only interested in opening an existing file, we set this parameter to False (which is also the default value).<\/P>\n<P>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\u2019s that easy. Leave the fourth parameter off and the file will be opened as ASCII; set the fourth parameter to -1 and &#8211; presto-changeo! &#8211; your file will be opened as Unicode,<\/P>\n<P>Thank you, thank you; you\u2019re too kind. How about a hand for our lovely assistant, Marjorie?<\/P>\n<P>What\u2019s that? Of <I>course<\/I> it works. Take a look at the message box we generate:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/unicode3.jpg\" width=\"170\" height=\"107\"> \n<P><BR>For our next trick, we will now make one of the Scripting Guys disappear and reappear at his son\u2019s 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\u2019t want bore you with a Scripting Dad bragging about his son. <I>&#8211; Editor\u2019s Note: Only the other Scripting Guys get that pleasure.<\/I>) See you tomorrow.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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?&#8212; FA Hey, FA. You know, the truly great magicians don\u2019t concoct elaborate tricks that rely on trap doors, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[40,3,4,14,5],"class_list":["post-67503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>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?&#8212; FA Hey, FA. You know, the truly great magicians don\u2019t concoct elaborate tricks that rely on trap doors, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67503"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67503\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}