DataFlex
DataFlex
Synchronize a Local Directory Tree to an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.SyncTreeUpload method, which synchronizes a local directory tree to a remote directory. The arguments are the local base directory, the remote base directory, the sync mode, and whether to recurse into subdirectories. The mode selects which files are uploaded (all, only-missing, only-newer, differing-size, and so on).
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: One call mirrors an entire directory to the server, which is the backbone of tasks like publishing a website or pushing a build. The mode is where the real decision lives: mode 0 re-sends everything, the newer-based modes rely on timestamps, and the size-difference modes (4 and 5) catch changed files even when timestamps are unreliable across systems — a common concern between platforms with different clock or filesystem behavior. Use
GetSyncedFiles afterward to see exactly what was transferred.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
Integer iPort
String sPassword
Integer iMode
Boolean iRecurse
String sTemp1
Move False To iSuccess
// Demonstrates the SFtp.SyncTreeUpload method, which synchronizes a local directory tree to a
// remote directory. The 1st argument is the local base directory, the 2nd is the remote base
// directory, the 3rd is the sync mode, and the 4th selects recursion into subdirectories.
Get Create (RefClass(cComChilkatSFtp)) To hoSftp
If (Not(IsComObjectCreated(hoSftp))) Begin
Send CreateComObject of hoSftp
End
// Connect, authenticate, and initialize the SFTP subsystem.
Move 22 To iPort
Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComInitializeSftp Of hoSftp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Sync mode 5 uploads files that are missing remotely or differ in size, plus files that are
// newer locally. Other modes:
// 0 = all files
// 1 = files not existing remotely
// 2 = files newer locally or missing remotely
// 3 = only files newer locally (do not upload missing)
// 4 = files missing remotely or differing in size
// 5 = mode 4 plus files newer locally
Move 5 To iMode
Move True To iRecurse
Get ComSyncTreeUpload Of hoSftp "qa_data/website" "/var/www/html" iMode iRecurse To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Directory tree uploaded."
Send ComDisconnect To hoSftp
End_Procedure