Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkStringTable.pb"
IncludeFile "CkSFtp.pb"

Procedure ChilkatExample()

    success.i = 0

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

    sftp.i = CkSFtp::ckCreate()
    If sftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Connect, authenticate, and initialize the SFTP subsystem.
    port.i = 22
    success = CkSFtp::ckConnect(sftp,"sftp.example.com",port)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  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.
    password.s = "mySshPassword"

    success = CkSFtp::ckAuthenticatePw(sftp,"mySshLogin",password)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    success = CkSFtp::ckInitializeSftp(sftp)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Perform a sync.
    mode.i = 5
    recurse.i = 1
    success = CkSFtp::ckSyncTreeDownload(sftp,"/var/www/html","qa_output/website",mode,recurse)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  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.
    synced.i = CkStringTable::ckCreate()
    If synced.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSFtp::ckGetSyncedFiles(sftp,synced)

    n.i = CkStringTable::ckCount(synced)
    Debug "Synced " + Str(n) + " item(s):"
    i.i
    For i = 0 To n - 1
        relPath.s = CkStringTable::ckStringAt(synced,i)
        If CkStringTable::ckLastMethodSuccess(synced) = 0
            Debug CkStringTable::ckLastErrorText(synced)
            CkSFtp::ckDispose(sftp)
            CkStringTable::ckDispose(synced)
            ProcedureReturn
        EndIf

        Debug relPath
    Next

    CkSFtp::ckDisconnect(sftp)


    CkSFtp::ckDispose(sftp)
    CkStringTable::ckDispose(synced)


    ProcedureReturn
EndProcedure