Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
integer li_Mode
integer li_Recurse
oleobject loo_Synced
integer n
integer i
string ls_RelPath

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

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Connect, authenticate, and initialize the SFTP subsystem.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Sftp.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Perform a sync.
li_Mode = 5
li_Recurse = 1
li_Success = loo_Sftp.SyncTreeDownload("/var/www/html","qa_output/website",li_Mode,li_Recurse)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  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.
loo_Synced = create oleobject
li_rc = loo_Synced.ConnectToNewObject("Chilkat.StringTable")

loo_Sftp.GetSyncedFiles(loo_Synced)

n = loo_Synced.Count
Write-Debug "Synced " + string(n) + " item(s):"

for i = 0 to n - 1
    ls_RelPath = loo_Synced.StringAt(i)
    if loo_Synced.LastMethodSuccess = 0 then
        Write-Debug loo_Synced.LastErrorText
        destroy loo_Sftp
        destroy loo_Synced
        return
    end if

    Write-Debug ls_RelPath
next

loo_Sftp.Disconnect()


destroy loo_Sftp
destroy loo_Synced