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

Control How FTP Directory Listings Are Requested

See more FTP Examples

Demonstrates the AllowMlsd, PreferNlst, and ListOption properties, which control which listing command Chilkat uses and what options it passes.

Background: FTP has three listing commands with different trade-offs. MLSD is machine-readable and standardized, so AllowMlsd (on by default) prefers it when the server supports it. When it must fall back to the legacy commands, PreferNlst chooses names-only NLST over the fuller LIST, and ListOption appends flags such as -a (to include hidden files) literally to LIST. Together they adapt listing to a particular server's capabilities and conventions.

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;
  n: Integer;

begin
  success := False;

  ftp := TFtp2.Create;

  ftp.Hostname := 'ftp.example.com';
  ftp.Username := 'myFtpLogin';

  //  AllowMlsd (default True) requests machine-readable MLSD listings when the server advertises
  //  support.  MLSD is preferred because its format is standardized.
  ftp.AllowMlsd := True;

  //  PreferNlst (default False) chooses NLST over LIST when a legacy listing is needed and MLSD is
  //  unavailable.  NLST returns only names.
  ftp.PreferNlst := False;

  //  ListOption is appended literally to a legacy LIST command; "-a" sends "LIST -a" to include
  //  hidden files on Unix servers.
  ftp.ListOption := '-a';

  //  Normally you would not hard-code the password in source.  You should instead obtain it
  //  from an interactive prompt, environment variable, or a secrets vault.
  ftp.Password := 'myPassword';

  success := ftp.Connect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  n := ftp.GetDirCount();
  WriteLn('Entries: ' + n);

  success := ftp.Disconnect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;


  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.