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

Get the Files Affected by an FTP Sync

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetSyncedFiles method, which reports the relative paths affected by the most recent synchronization. The only argument is a StringTable that receives the paths (uploaded, downloaded, or deleted).

Background: A sync call reports only overall success, but you usually want to know what actually changed — for logging, for triggering follow-up work on just the new files, or to confirm an incremental sync did the minimal transfer. An empty result is meaningful too: it means everything was already up to date. Call it immediately after the sync, before another operation replaces the recorded list.

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,
  Chilkat.StringTable;

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

procedure RunDemo;
var
  success: Boolean;
  ftp: TFtp2;
  mode: Integer;
  synced: TStringTable;
  n: Integer;
  i: Integer;
  relPath: string;

begin
  success := False;

  //  Demonstrates the Ftp2.GetSyncedFiles method, which reports the relative paths affected by the
  //  most recent synchronization operation.  The only argument is a StringTable that receives the
  //  paths.

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

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

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

  //  Perform a sync.
  mode := 6;
  success := ftp.SyncLocalTree('qa_output/site',mode);
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  //  Get the list of files that were uploaded, downloaded, or deleted by that sync.
  synced := TStringTable.Create;
  ftp.GetSyncedFiles(synced);

  n := synced.Count;
  WriteLn('Affected files: ' + n);

  for i := 0 to n - 1 do
    begin
      relPath := synced.StringAt(i);
      if (synced.LastMethodSuccess = False) then
        begin
          WriteLn(synced.LastErrorText);
          Exit;
        end;
      WriteLn(relPath);
    end;

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


  ftp.Free;
  synced.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.