July 07, 2024, 09:06:48 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Determining if file is a text file.

Started by LarryMc, December 16, 2010, 06:39:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

sapero

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.

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library