Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Sync a Local Tree Up with Preview and Traversal Control
See more FTP Examples
Demonstrates the Chilkat Ftp2.SyncRemoteTree2 method, which uploads a local tree to the current remote directory with extra controls. The arguments are the local root, the upload mode, whether to descend into subdirectories, and whether to run preview-only. In preview mode the results are available via the SyncPreview property.
Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: The preview flag is what makes this valuable: run it first to see exactly which files would be uploaded — without transferring anything — then run it again for real once the plan looks right. That dry-run step is a strong safeguard before a large or destructive-looking sync. The descend flag lets you limit the operation to the top level, and the mode is the same set of upload rules as
SyncRemoteTree.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.Ftp2;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ftp: TFtp2;
mode: Integer;
bDescend: Boolean;
bPreviewOnly: Boolean;
begin
success := False;
// Demonstrates the Ftp2.SyncRemoteTree2 method, which uploads a local tree to the current remote
// directory with extra traversal and preview controls. The 1st argument is the local root, the
// 2nd is the upload mode, the 3rd selects whether to descend into subdirectories, and the 4th
// selects preview-only (no actual transfers).
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;
// 0 = all selected files
// 1 = files missing on the server
// 2 = missing, or newer locally
// 3 = only files that exist remotely and are newer locally
// 4 = missing, or a different size
// 5 = missing, different size, or newer locally
mode := 5;
// Descend into subdirectories.
bDescend := True;
// Preview only: determine what WOULD be uploaded without transferring anything. The results are
// available via the SyncPreview property.
bPreviewOnly := True;
success := ftp.SyncRemoteTree2('qa_data/site',mode,bDescend,bPreviewOnly);
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
// SyncPreview lists the files that would be uploaded.
WriteLn(ftp.SyncPreview);
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.