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

Send FTP STAT and Get the Reply

See more FTP Examples

Demonstrates the Chilkat Ftp2.Stat method, which sends the FTP STAT command and returns the server's reply. It takes no arguments; depending on the server and session state, the reply may contain status information or a directory listing.

Background: STAT is unusual in that it returns over the control connection without opening a data connection, which is why some clients use STAT as a lightweight directory listing that sidesteps passive/active data-channel setup. Its exact behavior, accepted arguments, and whether it works before login are all server-dependent, so treat the reply as advisory rather than a fixed format.

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;
  statReply: string;

begin
  success := False;

  //  Demonstrates the Ftp2.Stat method, which sends the FTP STAT command and returns the server's
  //  reply.  It takes no arguments.  Depending on the server, the reply may contain status
  //  information or a directory listing.

  ftp := TFtp2.Create;

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

  //  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';

  //  Connect and log in (Connect performs both).
  success := ftp.Connect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  statReply := ftp.Stat();
  if (ftp.LastMethodSuccess = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  WriteLn(statReply);

  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.