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

Filter Files and Directories in an FTP Sync

See more FTP Examples

Demonstrates the synchronization filter properties SyncMustMatch, SyncMustNotMatch, SyncMustMatchDir, SyncMustNotMatchDir, and SyncCreateAllLocalDirs.

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 real sync rarely wants every file. These semicolon-separated wildcard filters shape the set: the MustMatch pairs include only matching files/directories, while the MustNotMatch pairs exclude them — letting you, say, publish only web assets while skipping build artifacts and caches. Directory filters also prune traversal, avoiding wasted time descending into folders you do not need. They apply to the tree-sync methods such as SyncLocalTree and SyncRemoteTree.

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;
  mode: Integer;

begin
  success := False;

  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;

  //  Include only files matching these semicolon-separated, case-insensitive wildcard patterns.
  ftp.SyncMustMatch := '*.html;*.css;*.js';

  //  Exclude files matching these patterns, even if they matched SyncMustMatch.
  ftp.SyncMustNotMatch := '*.tmp;*.bak';

  //  Restrict which directories are traversed (and which are skipped) during a tree sync.
  ftp.SyncMustMatchDir := 'assets;pages';
  ftp.SyncMustNotMatchDir := 'cache;node_modules';

  //  For a download sync, create empty remote directories on the local side too.
  ftp.SyncCreateAllLocalDirs := True;

  //  The filters apply to the tree sync operations, such as SyncLocalTree.
  mode := 6;
  success := ftp.SyncLocalTree('qa_output/site',mode);
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  WriteLn('Filtered sync complete.');

  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.