Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Get Number of FIles in Directory, not including sub-directories

See more FTP Examples

_LANGUAGE_ example demonstrating how to get the number of files in a directory not including sub-directories.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Ftp2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ftp: TFtp2;
  i: Integer;
  n: Integer;
  fileCount: Integer;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  ftp := TFtp2.Create;

  ftp.Hostname := 'ftp.example.com';
  ftp.Username := 'login';
  ftp.Password := 'password';

  //  Connect and login to the FTP server.
  success := ftp.Connect();
  if (success <> True) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  //  The ListPattern property is our directory listing filter.
  //  The default value is "*", which includes everything.
  WriteLn(ftp.ListPattern);

  //  Fetch the current remote directory contents by
  //  calling GetDirCount

  n := ftp.GetDirCount();
  if (n < 0) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  if (n > 0) then
    begin
      //  Loop over the directory contents, incrementing the count
      //  each time it is NOT a directory.
      fileCount := 0;
      for i := 0 to n - 1 do
        begin

          //  Is this NOT a sub-directory?
          if (ftp.GetIsDirectory(i) <> True) then
            begin
              fileCount := fileCount + 1;
              //  Display the filename
              WriteLn(ftp.GetFilename(i));
            end;

        end;

      WriteLn('Total number of files = ' + fileCount);
    end;

  success := ftp.Disconnect();


  ftp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.