Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Create an FTP Upload Plan
See more FTP Examples
Demonstrates the Chilkat Ftp2.CreatePlan method, which creates and returns a textual upload plan for a local directory. The only argument is the local directory. The object must be connected, because the plan records the current absolute remote directory together with the local source paths.
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: A plan is a saved, resumable description of an upload — the list of local files and their absolute remote destinations — computed once and executed later (or on another run) with
PutPlan. Separating planning from execution suits large or unreliable transfers: the plan can be reviewed, stored, and re-run, and because it records absolute remote paths it stays correct even if the working directory later changes.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,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ftp: TFtp2;
plan: string;
sbPlan: TStringBuilder;
begin
success := False;
// Demonstrates the Ftp2.CreatePlan method, which creates and returns a textual upload plan for a
// local directory. The only argument is the local directory. The object must be connected,
// because the plan records the current absolute remote directory.
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;
// The plan captures which local files to upload and the absolute remote destination. Save it so
// the upload can be run (or resumed) later with PutPlan.
plan := ftp.CreatePlan('qa_data/site');
if (ftp.LastMethodSuccess = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
// Persist the plan text for later use.
sbPlan := TStringBuilder.Create;
sbPlan.Append(plan);
success := sbPlan.WriteFile('qa_output/upload_plan.txt');
if (success = False) then
begin
WriteLn(sbPlan.LastErrorText);
Exit;
end;
WriteLn('Upload plan created.');
success := ftp.Disconnect();
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
ftp.Free;
sbPlan.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.