IonicWind Software

IWBasic => General Questions => Topic started by: LarryMc on December 16, 2010, 06:39:20 PM

Title: Determining if file is a text file.
Post by: LarryMc on December 16, 2010, 06:39:20 PM
Is there some neat way to determine if a file is a text file other than the brute force checking the of file extension against a list of extensions?
if (instr(".exe.obj.lib.dll.jpg.bmp.png.zip.dbgs.dat.iwp.ebp.vdp.res.chm.pdf.hlp",ext)=0) and (ext<>""))
   'must be a text file
endif


LarryMc
Title: Re: Determining if file is a text file.
Post by: sapero on December 17, 2010, 12:08:24 AM
Hi,

A dirty methid: check the "Content Type" value in HKEY_CLASSES_ROOT\.txt.
text/plainThe first part will be "text" for text files.
For other file extensions just replace \.txt with the target extension.

Note, that some registered files have not associated Content Type. In this case you need to parse the files:
a) if the file starts with "\xFF\xFE", it is encoded in unicode format (two bytes per character)
b) if the file starts with "\xFE\xFF", it is encoded in unicode big-endian format (two reversed bytes per character)
c) if the file starts with "\xEF\xBB\xBF", it is encoded in UFT-8 format (8,16,24,32... bytes per character)

a,b,c - there should be no NULL character.
Title: Re: Determining if file is a text file.
Post by: LarryMc on December 18, 2010, 11:26:07 AM
Sapero

Reading the entire file into memory then checking for char=0 works great.
If there is no null character that tells me it is okay to lread the file as a text file.
It does exactly what I want it to do.

Thanks

LarryMc