{"id":2645,"date":"2015-11-11T10:39:26","date_gmt":"2015-11-11T10:39:26","guid":{"rendered":"https:\/\/www.mjtnet.com\/blog\/?p=2645"},"modified":"2015-11-11T13:05:20","modified_gmt":"2015-11-11T13:05:20","slug":"convert-file-to-utf8","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2015\/11\/11\/convert-file-to-utf8\/","title":{"rendered":"Fix Unicode file with missing BOM"},"content":{"rendered":"<p><a href=\"https:\/\/www.mjtnet.com\/macro-scheduler.htm\">Macro Scheduler<\/a>&#8216;s ReadFile and ReadLn functions understand ANSI, UTF8 and Unicode files &#8211; as long as they have a valid <a href=\"https:\/\/en.wikipedia.org\/wiki\/Byte_order_mark\">BOM header<\/a>. \u00a0But a client recently needed to read in a file with a missing BOM. \u00a0So we wrote a little bit of VBScript which reads the binary stream in, and then outputs a UTF8 encoded file. \u00a0<\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre class=\"brush:macroscript\">VBSTART\r\nSub UTFConvert(filename)\r\n  Set fso = CreateObject(\"Scripting.FileSystemObject\")\r\n  txt = fso.OpenTextFile(filename, 1, False, -1).ReadAll\r\n  Set stream = CreateObject(\"ADODB.Stream\")\r\n  stream.Open\r\n  stream.Type     = 2 'text\r\n  stream.Position = 0\r\n  stream.Charset  = \"utf-8\"\r\n  stream.WriteText txt\r\n  stream.SaveToFile filename, 2\r\n  stream.Close\r\nEnd Sub\r\nVBEND\r\n\r\n\/\/Convert it to UTF8\r\nVBRun>UTFConvert,%SCRIPT_DIR%\\data.txt\r\n\r\n\/\/Now we can read it :-)\r\nReadFile>%SCRIPT_DIR%\\data.txt,theFileData<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Macro Scheduler&#8216;s ReadFile and ReadLn functions understand ANSI, UTF8 and Unicode files &#8211; as long as they have a valid BOM header. \u00a0But a client recently needed to read in a file with a missing BOM. \u00a0So we wrote a little bit of VBScript which reads the binary stream in, and then outputs a UTF8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2645"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=2645"}],"version-history":[{"count":8,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2645\/revisions"}],"predecessor-version":[{"id":2653,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2645\/revisions\/2653"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=2645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=2645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=2645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}