1. Home
  2. Computing & Technology
  3. Delphi Programming

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

By , About.com Guide

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)

Delphi tips navigator:
» Get the Application associated with the Shell Print Command for a File Type from Delphi
« Class Helper for Delphi's TStrings: Implemented Add(Variant)

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2008 Tips
  7. File Size - Get the Size of a File in Bytes using Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.