BTW, I know about the ReadStringFormat command and the #PB_Ascii
result, but as the docs say: that isn't 100% foolproof or a file standard.
Also, don't forget that a file called test.txt with a 0 byte size is not really
a text file, so there is no bug with this procedure on such files.

[Edit] See my enhanced version further down which doesn't use
AllocateMemory, PeekS and FileSeek. Thanks to AND51 for that!
Code: Select all
; IsTextFile by PB. Free for any use without credit needed. :)
; Reads the first 128 bytes of file$ to see if it's text or binary.
; If it contains Chr(0) then it's considered binary, as text files
; don't have it. Works fine with files less than 128 bytes in size.
; Probably not 100% foolproof but haven't had a false positive yet.
; Example below is for Windows, but the procedure is cross-platform.
; Return values: -1 if can't get a result, 0 for binary, 1 for text.
Procedure IsTextFile(file$)
v=-1
If ReadFile(0,file$)
m=AllocateMemory(128)
If m
v=0 : ReadData(0,m,128) : m$=PeekS(m) : FreeMemory(m)
FileSeek(0,0) : If m$<>ReadString(0) : v=1 : EndIf
EndIf
CloseFile(0)
EndIf
ProcedureReturn v
EndProcedure
windir$=Space(999) : GetWindowsDirectory_(windir$,999)
If Right(windir$,1)<>"\" : windir$+"\" : EndIf
If ExamineDirectory(0,windir$,"*.*")
While NextDirectoryEntry(0)
type=DirectoryEntryType(0)
name$=DirectoryEntryName(0)
If type=#PB_DirectoryEntry_File
Debug name$+" = "+Str(IsTextFile(windir$+name$))
EndIf
Wend
FinishDirectory(0)
EndIf