Mega Code Archive

 
Categories / Delphi / Files
 

Check if a file is a text file or a binary one

Title: check if a file is a text file or a binary one? function IsTextFile(const sFile: TFileName): boolean; //Created By Marcelo Castro - from Brazil var oIn: TFileStream; iRead: Integer; iMaxRead: Integer; iData: Byte; dummy:string; begin result:=true; dummy :=''; oIn := TFileStream.Create(sFile, fmOpenRead or fmShareDenyNone); try iMaxRead := 1000; //only text the first 1000 bytes if iMaxRead oIn.Size then iMaxRead := oIn.Size; for iRead := 1 to iMaxRead do begin oIn.Read(iData, 1); if (idata) 127 then result:=false; end; finally FreeAndNil(oIn); end; end; (* ----- Sample call ----- *) procedure TForm1.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then begin if IsTextFile(OpenDialog1.FileName) then showmessage('is ascii') else showmessage('is BinaryFile') end; end;