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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary or Text file?

Status
Not open for further replies.

josemauricio

Programmer
Sep 2, 2003
65
BR
Hey guys!

How can I know if a file is a binary or a text file?

Thanx.

Zé Maurício.
 
There's no reliable way to determine this.

The file could be a UTF-8 Unicode file with a byte-order-mark (three byte flag) at the front of it, and that could cause you to think it's a binary file, when it really is text, just not plain ascii text.

A technique I've seen used is to scan through the first 1000 bytes, counting up printable characters vs. non-printing characters (carriage-returns, etc fall into the non-printing category). You'd then decide on a percentage of non-printing characters to mark your tipping point -- above that quantity, the file is assumed to be binary. Below that amount, you assume it's text.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Would just checking the file extension give you what you want? If so I use

Dim filename as String

Filename = "test.txt"

filename = filename.Substring(filename.LastIndexOf(".") + 1, filename.Length - filename.LastIndexOf(".") - 1)
filename = filename.ToUpper
Select Case filename
Case "TXT"
Binary = False
Return
Case Else
Binary = True
Return
End Select

 
Thank you both for reply!!

Kevin, I'd like to know the sort of data of the file. htm, xml, txt, and others are text files extensions.

José Maurício.
 
Thats fine just add another CASE for those that you know are text. i.e

Case "HTML"
Binary = False
Return
Case "XML"
Binary = False
Return
Case "HTM"
Binary = False
Return

etc.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top