Name

FileSize Function

Syntax

function FileSize(var F: File): LongInt;
function FileSize(var F: TextFile): LongInt;

Description

FileSize returns the size in records of the file F. If F is a TextFile, the record size is arbitrarily chosen as the buffer size, which defaults to 128. If F is an untyped binary file, the record size is determined when the file is opened. FileSize is not a real function.

Errors

  • If the file F is not open, FileSize reports I/O error 103.

  • Real text files don’t have fixed-size records, so FileSize is useless for text files. Use streams or call the Windows API function GetFileSize instead of calling FileSize.

// Return the size in bytes of a text file or -1 for an error.
function TextFileSize(var F: TextFile): LongInt;
begin
  case TTextRec(F).Mode of
    fmInput, fmOutput: Result := GetFileSize(TTextRec(F).Handle, nil);
    else               Result := -1;
  end;
end;
  • FileSize does not support files larger than 2 GB. See the FileSeek function in the SysUtils unit, or call the Windows API for large files.

See Also

Eof Function, File Keyword, FilePos Function, IOResult Function, Reset Procedure, Rewrite Procedure, Seek Procedure, TextFile Type, Truncate Procedure

Get Delphi in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.