Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine file type

Status
Not open for further replies.

sn0rg

IS-IT--Management
Aug 5, 2005
95
0
0
GB
Hi guys

I am opening text files in vbscript, and need to discover if I should use TriStateTrue or TriStateFalse.

e.g.
Code:
Set objInputFile = objFSO.OpenTextFile(strFilePath, ForReading, False, TristateTrue)


is there a way to interrogate the file before I open it? If I open in the wrong format, the output is garbage.
 
Const ForReading = 1
Const TristateFalse = 0

Function IsUnicode(strFile)
With CreateObject("scripting.filesystemobject")
IsUnicode = (.OpenTextFile(strFile, ForReading, , TristateFalse).Read(2) = Chr$(255) & Chr$(254))
End With
End Function
 
(you may also want to Google "Unicode BOM" to understand what my code is actually doing, and thus its limitations)
 
Thanks for your help strongm, but I'm struggling. I get an error of "Microsoft VBScript compilation error: Invalid character" when the "isunicode =" line runs.

I made the following script based on your response:
Code:
	Const ForReading = 1, ForWriting = 2
	Const TriStateTrue = -1, TriStateUseDefault = -2, TristateFalse = 0

strFile = "C:\file.log"

if IsUnicode = True then
	wscript.echo "True"
Else
	wscript.echo "False"
End if


Function IsUnicode(strFile) 
	With CreateObject("scripting.filesystemobject")        
		IsUnicode = (.OpenTextFile(strFile, ForReading, ,TriStateFalse).Read(2) = Chr$(255) & Chr$(254))
	End With
End Function
 
How about:
Code:
Const ForReading = 1, ForWriting = 2
    Const TriStateTrue = -1, TriStateUseDefault = -2, TristateFalse = 0

strFile = "C:\file.log"

if IsUnicode(strFile) = True then
    wscript.echo "True"
Else
    wscript.echo "False"
End if


Function IsUnicode(strFile)
    With CreateObject("scripting.filesystemobject")        
        IsUnicode = (.OpenTextFile(strFile, ForReading, ,TriStateFalse).Read(2) = Chr(255) & Chr(254))
    End With
End Function
Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Miles faster than me tsuji! [smile]

But I did catch the function call... [wink]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Chr vs Chr$ - see, that's my VB, rather than VBScript background showing through. ANd there I was thinking I'd caught all the necessary changes before posting ...
 
Thanks guys... feeling somewhat embarrassed that I didn't spot the $, but it works great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top