Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFtp
    Integer iMode
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatFtp2)) To hoFtp
    If (Not(IsComObjectCreated(hoFtp))) Begin
        Send CreateComObject of hoFtp
    End

    Set ComHostname Of hoFtp To "ftp.example.com"
    Set ComUsername Of hoFtp To "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.
    Set ComPassword Of hoFtp To "myPassword"

    Get ComConnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComChangeRemoteDir Of hoFtp "public_html" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Include only files matching these semicolon-separated, case-insensitive wildcard patterns.
    Set ComSyncMustMatch Of hoFtp To "*.html;*.css;*.js"

    //  Exclude files matching these patterns, even if they matched SyncMustMatch.
    Set ComSyncMustNotMatch Of hoFtp To "*.tmp;*.bak"

    //  Restrict which directories are traversed (and which are skipped) during a tree sync.
    Set ComSyncMustMatchDir Of hoFtp To "assets;pages"
    Set ComSyncMustNotMatchDir Of hoFtp To "cache;node_modules"

    //  For a download sync, create empty remote directories on the local side too.
    Set ComSyncCreateAllLocalDirs Of hoFtp To True

    //  The filters apply to the tree sync operations, such as SyncLocalTree.
    Move 6 To iMode
    Get ComSyncLocalTree Of hoFtp "qa_output/site" iMode To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Filtered sync complete."

    Get ComDisconnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure