Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 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.

set sftp [new_CkSFtp]

#  Connect, authenticate, and initialize the SFTP subsystem.
set port 22
set success [CkSFtp_Connect $sftp "sftp.example.com" $port]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

#  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 password "mySshPassword"

set success [CkSFtp_AuthenticatePw $sftp "mySshLogin" $password]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

set success [CkSFtp_InitializeSftp $sftp]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

#  Perform a sync.
set mode 5
set recurse 1
set success [CkSFtp_SyncTreeDownload $sftp "/var/www/html" "qa_output/website" $mode $recurse]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

#  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.
set synced [new_CkStringTable]

CkSFtp_GetSyncedFiles $sftp $synced

set n [CkStringTable_get_Count $synced]
puts "Synced $n item(s):"

for {set i 0} {$i <= [expr $n - 1]} {incr i} {
    set relPath [CkStringTable_stringAt $synced $i]
    if {[CkStringTable_get_LastMethodSuccess $synced] == 0} then {
        puts [CkStringTable_lastErrorText $synced]
        delete_CkSFtp $sftp
        delete_CkStringTable $synced
        exit
    }

    puts "$relPath"
}

CkSFtp_Disconnect $sftp

delete_CkSFtp $sftp
delete_CkStringTable $synced