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

Get FTP Directory Listing Information

See more FTP Examples

_LANGUAGE_ example showing how to get information about files and subdirectories in the current remote FTP directory.

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;

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);

  //  To get file and sub-directory information, simply
  //  loop from 0 to ftp.GetDirCount() - 1

  n := ftp.GetDirCount();
  if (n < 0) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  if (n > 0) then
    begin
      for i := 0 to n - 1 do
        begin

          //  Display the filename
          WriteLn(ftp.GetFilename(i));

          //  Display the file size (in bytes)
          WriteLn(ftp.GetSize(i));

          //  Is this a sub-directory?
          if (ftp.GetIsDirectory(i) = True) then
            begin
              WriteLn('.. this is a sub-directory');
            end;

          WriteLn('--');
        end;

    end;

  WriteLn('-----------------------------------');

  //  Only files and directories
  //  matching the ListPattern are returned.
  ftp.ListPattern := '*.asp';
  n := ftp.GetDirCount();
  if (n < 0) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  if (n > 0) then
    begin
      for i := 0 to n - 1 do
        begin

          //  Display the filename
          WriteLn(ftp.GetFilename(i));

          //  Display the file size (in bytes)
          WriteLn(ftp.GetSize(i));

          WriteLn('--');
        end;

    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.