Thanks Chip,
for the suggestion about only testing the first character. I'd have to try that 
one, and see if that will ensure the app running on all machines.

As for the CreateTextFile, YES, it definitely can be used to both read and 
write. Works here, and the docs state it to do so as well. Below, I have pasted 
the info found on Microsoft's page, and also given you the link. For others, 
there is even a short sample code at the bottom.

http://www.piclist.com/techref/language/asp/vbs/vbscript/229.htm

CreateTextFile Method
Description
Creates a specified file name and returns a TextStream object that can be used 
to
read from or write to the file.
Syntax
[object.]CreateTextFile(filename[, overwrite[, unicode]])
The CreateTextFile method has these parts:
Argument
Description
object
Optional. Object is always the name of a FileSystemObject.
filename
Required.
String expression
 that identifies the file to create.
overwrite
Optional. Boolean value that indicates whether you can overwrite an existing 
file.
The value is
True if the file can be overwritten, False
 if it can't be overwritten. If omitted, existing files are not overwritten.
unicode
Optional. Boolean value that indicates whether the file is created as a Unicode 
or
ASCII file. The value is
True if the file is created as a Unicode file, False
 if it's created as an ASCII file. If omitted, an ASCII file is assumed.

  ----- Original Message ----- 
  From: Chip Orange 
  To: [email protected] 
  Sent: Friday, October 28, 2011 1:17 PM
  Subject: RE: How to detect textfile format?


  David,

  there's a third variation of encoding, whose name escapes me at the moment 
(although it may be ANSI), but is read with the "unicode" option.  That's why I 
only test for the first character being "ff" in my example, as it covers both 
variations that way.

  Chip




------------------------------------------------------------------------------
  From: David [mailto:[email protected]] 
  Sent: Friday, October 28, 2011 12:03 AM
  To: [email protected]
  Subject: Re: How to detect textfile format?


  Thanks Jeff.
  It somehow got up working now. But I had a bit of a hard time, as it does 
differ a bit from your code. See, your code states, that the Hex-number should 
be FFFE. But on my system, if I want to get it working, I have to test for a 
HEX-number of FF0. 

  Puzzled why, but that is the way it is. Wonder if my script then will work on 
other computers, since there could be the difference in the testing number. 
Anyone could shed some light on that one?


    ----- Original Message ----- 
    From: Jeff Weiss 
    To: [email protected] 
    Sent: Friday, October 28, 2011 4:53 AM
    Subject: Re: How to detect textfile format?


    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.

Reply via email to