Mega Code Archive

 
Categories / Delphi / Files
 

File Size Get the Size of a File in Bytes using Delphi

Title: File Size - Get the Size of a File in Bytes using Delphi When working with files and folders from a Delphi application you sometimes need to know what is the size of a file (in bytes). Get File Size The FileSize function returns the size of a file in bytes, -1 if the file was not found. // returns file size in bytes or -1 if not found. function FileSize(fileName : wideString) : Int64; var sr : TSearchRec; begin if FindFirst(fileName, faAnyFile, sr ) = 0 then result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow) else result := -1; FindClose(sr) ; end; Note: when you have the size of a file in bytes, you might want to format the size for display (Kb, Mb, Gb)