Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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.
let sftp = CkoSFtp()!
// Connect, authenticate, and initialize the SFTP subsystem.
var port: Int = 22
success = sftp.connect(hostname: "sftp.example.com", port: port)
if success == false {
print("\(sftp.lastErrorText!)")
return
}
// 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.
var password: String? = "mySshPassword"
success = sftp.authenticatePw(login: "mySshLogin", password: password)
if success == false {
print("\(sftp.lastErrorText!)")
return
}
success = sftp.initializeSftp()
if success == false {
print("\(sftp.lastErrorText!)")
return
}
// Perform a sync.
var mode: Int = 5
var recurse: Bool = true
success = sftp.syncTreeDownload(remoteRoot: "/var/www/html", localRoot: "qa_output/website", mode: mode, recurse: recurse)
if success == false {
print("\(sftp.lastErrorText!)")
return
}
// 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.
let synced = CkoStringTable()!
sftp.getSyncedFiles(strTab: synced)
var n: Int = synced.count.intValue
print("Synced \(n) item(s):")
var i: Int
for i = 0; i <= n - 1; i++ {
var relPath: String? = synced.string(at: i)
if synced.lastMethodSuccess == false {
print("\(synced.lastErrorText!)")
return
}
print("\(relPath!)")
}
sftp.disconnect()
}