SmartPascal
FileSize
Function
Gives the size in records of an open file System unit
 function FileSize ( var FileHandle : File; ) : Integer;
Description
The FileSize function gives the size in records of an open file.
 
Before this function can be used, the file must be assigned a handle by using AssignFile and opened using Append, Reset or ReWrite routines.
Notes
Because text files are normally variable in length, the number of records is not so useful. Use the low level routine GetFileSize to get the size in bytes, or FindFirst, FindNext routines, which return size and last modified date values of one or more files.
Related commands
DiskFree Gives the number of free bytes on a specified drive
DiskSize Gives the size in bytes of a specified drive
 
Example code : Get the size in records of a typed binary file
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myWord, myWord1, myWord2, myWord3 : Word;
  myFile : File of Word;

begin
  // Try to open the Test.cus binary file in write only mode
  AssignFile(myFile, 'Test.cus');
  ReWrite(myFile);

  // Before writing to the file, show the file size
  ShowMessage('File size = '+IntToStr(FileSize(myFile)));

  // Write a few lines of Word data to the file
  myWord1 := 123;
  myWord2 := 456;
  myWord3 := 789;
  Write(myFile, myWord1, myWord2, myWord3);

  // Before closing the file, show the new file size
  ShowMessage('File size now = '+IntToStr(FileSize(myFile)));

  // Close the file
  CloseFile(myFile);
end;
 
end.
Hide full unit code
   File size = 0
   File size now = 3