Convert File to UTF8

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Convert File to UTF8

Post by Marcus Tettmar » Wed Nov 11, 2015 10:47 am

We recently needed to read in a UCS-2 encoded file with a missing BOM. So we used a little bit of VBScript to convert the file to UTF-8:

Code: Select all

VBSTART
Sub UTFConvert(filename)
  Set fso = CreateObject("Scripting.FileSystemObject")
  txt = fso.OpenTextFile(filename, 1, False, -1).ReadAll
  Set stream = CreateObject("ADODB.Stream")
  stream.Open
  stream.Type     = 2 'text
  stream.Position = 0
  stream.Charset  = "utf-8"
  stream.WriteText txt
  stream.SaveToFile filename, 2
  stream.Close
End Sub
VBEND

//Convert it to UTF8
VBRun>UTFConvert,%SCRIPT_DIR%\data.txt

//Now we can read it :-)
ReadFile>%SCRIPT_DIR%\data.txt,theFileData
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts