Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSftp
    Integer iPort
    String sPassword
    Integer iMode
    Boolean iRecurse
    Variant vSynced
    Handle hoSynced
    Integer n
    Integer i
    String sRelPath
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatSFtp)) To hoSftp
    If (Not(IsComObjectCreated(hoSftp))) Begin
        Send CreateComObject of hoSftp
    End

    //  Connect, authenticate, and initialize the SFTP subsystem.
    Move 22 To iPort
    Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComInitializeSftp Of hoSftp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Perform a sync.
    Move 5 To iMode
    Move True To iRecurse
    Get ComSyncTreeDownload Of hoSftp "/var/www/html" "qa_output/website" iMode iRecurse To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    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.
    Get Create (RefClass(cComChilkatStringTable)) To hoSynced
    If (Not(IsComObjectCreated(hoSynced))) Begin
        Send CreateComObject of hoSynced
    End
    Get pvComObject of hoSynced to vSynced
    Send ComGetSyncedFiles To hoSftp vSynced

    Get ComCount Of hoSynced To n
    Showln "Synced " n " item(s):"

    For i From 0 To (n - 1)
        Get ComStringAt Of hoSynced i To sRelPath
        Get ComLastMethodSuccess Of hoSynced To bTemp1
        If (bTemp1 = False) Begin
            Get ComLastErrorText Of hoSynced To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Showln sRelPath
    Loop

    Send ComDisconnect To hoSftp


End_Procedure