Here are some lines from one of my apps which should do what you need:
Const ForReading = 1
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
' determine whether the file is Unicode or not.
Dim ObjScriptFile : Set objScriptFile = FSO.OpenTextFile(Path & nString &
".vbs")
Dim firstByte, secondByte
firstByte = Hex(AscB(MidB(objScriptFile.Read(1), 1, 1)))
secondByte = Hex(AscB(MidB(objScriptFile.Read(1), 1, 1)))
If Err.Number > 0 Then
Mess =Mess & " " & "Error " & Err.Number & " " & Err.Description & VbCrLf
If Err.Number = 424 Then
Mess = Mess & "File not Found! "
End If
On Error goto 0
Exit Function
End If
If firstByte & secondByte = "FFFE" Then
' unicode file
strContents = SOFileToString(Path & nString & ".vbs", "Unicode")
Else
' not unicode
strContents = SOFileToString(Path & nString & ".vbs", "")
End If
objScriptFile.Close
hth
Jeff Weiss
----- Original Message -----
From: David
To: [email protected]
Sent: Thursday, October 27, 2011 9:36 PM
Subject: How to detect textfile format?
In my script, I want to read the information from a text file.
As it is going to read from any text file on the disk, I have no clue whether
the actual text file will be an ASCII, or an Unicode file. I am using the
CreateTextFile method to open the text file. This method does require me to
determine if I am going to open one or the other of the file formats. Is there
any good work around for this in VBS? Some way for me to open a text file, and
have it properly read, no matter which of the two formats it has been saved in?
From my testing here, I get the following results. Try to open an ASCII file,
having CreateTextFile set to UniCode, only gives me one line, and no recognized
text.
Try to open an UniCode file, having CreateTextFile set to ASCII, will
recognize all the lines, but does not work properly when I try to perform
textual operations; like Mid, and InStr.
Yep, even computers have their multi-lingual challenges. Smile.
Anyone has a good idea here, I would be thankful.