Swift
Swift
SFTP Download Files Matching a Pattern
See more SFTP Examples
Demonstrates how to download files in a directory matching one or more patterns (such as "*.zip" or "abc*_*0719.csv".Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let sftp = CkoSFtp()!
success = sftp.connect(hostname: "my-ssh-server.com", port: 22)
if success == true {
success = sftp.authenticatePw(login: "mySshLogin", password: "mySshPassword")
}
if success == true {
success = sftp.initializeSftp()
}
if success != true {
print("\(sftp.lastErrorText!)")
return
}
// The SyncTreeDownload method can be used non-recursively to download all files matching a set of patterns.
// This example will download all files, but only those files having filenames
// that match *.csv and *.eml
sftp.syncMustMatch = "*.eml; *.gif"
var remoteDir: String? = "syncDownloadTest/someDir"
var localDir: String? = "qa_output"
// mode=0: Download all matching files according to SyncMustMatch
var mode: Int = 0
// do not recursively traverse the remote directory tree.
var recursive: Bool = false
success = sftp.syncTreeDownload(remoteRoot: remoteDir, localRoot: localDir, mode: mode, recurse: recursive)
if success != true {
print("\(sftp.lastErrorText!)")
return
}
print("Success.")
}