VB Script Reading Text File with UTF-8 BOM Encoding

VB Script Reading Text File with UTF-8 BOM Encoding

It was brought to our attention that reading the contents of a text file via VB Script (as described here  - VB Script - Read and Write Text file using an Array (qxcmm.com)) will be problematic if the text file encoding is UTF-8 BOM.  This BOM encoding adds 3 Bytes of "garbage" data to the beginning of the Data Stream that shows up in VB Variables / Strings as shown below:



I found a solution using ADO Objects as follows:
  1. 'open Unicode file
  2. Set objFile = CreateObject("ADODB.Stream")

  3. objFile.CharSet = "utf-8"
  4. objFile.Open
  5. objFile.LoadFromFile(MyFile)

  6.   Line1 = objFile.ReadText(-2)
  7.   Line3 = objFile.ReadText(-2)
  8.   Line2 = objFile.ReadText(-2)
  9.   Line4 = objFile.ReadText(-2)
  10. objFile.Close
  11. Set objFile = Nothing
 
Thanks to StackOverflow for this solution - utf 8 - Read utf-8 text file in vbscript - Stack Overflow