Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get the Files Transferred by an SFTP Sync
See more SFTP Examples
Demonstrates the Chilkat SFtp.GetSyncedFiles method, which reports the relative paths processed by the most recent SyncTreeUpload or SyncTreeDownload. The only argument is a StringTable that receives the paths. Directory paths end with / so they can be distinguished from files.
Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.
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 simply to confirm an incremental sync did the minimal transfer. This method provides that list after the fact. 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
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.SFtp,
Chilkat.StringTable;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sftp: TSFtp;
port: Integer;
password: string;
mode: Integer;
recurse: Boolean;
synced: TStringTable;
n: Integer;
i: Integer;
relPath: string;
begin
success := False;
// Demonstrates the SFtp.GetSyncedFiles method, which reports the relative paths processed by the
// most recent SyncTreeUpload or SyncTreeDownload. The only argument is a StringTable that
// receives the paths.
sftp := TSFtp.Create;
// Connect, authenticate, and initialize the SFTP subsystem.
port := 22;
success := sftp.Connect('sftp.example.com',port);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// 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.
password := 'mySshPassword';
success := sftp.AuthenticatePw('mySshLogin',password);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
success := sftp.InitializeSftp();
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Perform a sync.
mode := 5;
recurse := True;
success := sftp.SyncTreeDownload('/var/www/html','qa_output/website',mode,recurse);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Get the list of files (and, for downloads, directories) that were actually transferred.
// Directory paths end with "/" so they can be told apart from file paths.
synced := TStringTable.Create;
sftp.GetSyncedFiles(synced);
n := synced.Count;
WriteLn('Synced ' + n + ' item(s):');
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;
sftp.Disconnect();
sftp.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.