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:
- 'open Unicode file
- Set objFile = CreateObject("ADODB.Stream")
- objFile.CharSet = "utf-8"
- objFile.Open
- objFile.LoadFromFile(MyFile)
- Line1 = objFile.ReadText(-2)
- Line3 = objFile.ReadText(-2)
- Line2 = objFile.ReadText(-2)
- Line4 = objFile.ReadText(-2)
- objFile.Close
- Set objFile = Nothing