Mega Code Archive

 
Categories / Delphi / Files
 

An easier function to get a files Size

Title: An easier function to get a file's Size. Question: How to retrieve a file's size given it's name, no handle asked ;-) Answer: I have written this small, little and very fast function that returns the size of a file, given the name and path. If the file doesn't exists, it returns -1 instead of the file's size, so you can also catch errors in passing the filename. //======================================================== function GetFileSize(FileName: String): Integer; var FS: TFileStream; begin try FS := TFileStream.Create(Filename, fmOpenRead); except Result := -1; end; if Result -1 then Result := FS.Size; FS.Free; end; //======================================================== ========================================================== Example of use: ========================================================== procedure TForm1.FormCreate(Sender: TObject); begin ShowMessage(IntToStr(GetFileSize('c:\prova.pdf'))); end; ==========================================================