Sample code for 30+ languages & platforms
C

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

C
#include <C_CkSFtp.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSFtp sftp;
    int port;
    const char *password;
    int mode;
    BOOL recurse;

    success = FALSE;

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

    sftp = CkSFtp_Create();

    //  Connect, authenticate, and initialize the SFTP subsystem.
    port = 22;
    success = CkSFtp_Connect(sftp,"sftp.example.com",port);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        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.
    password = "mySshPassword";

    success = CkSFtp_AuthenticatePw(sftp,"mySshLogin",password);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    success = CkSFtp_InitializeSftp(sftp);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    //  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
    mode = 5;
    recurse = TRUE;
    success = CkSFtp_SyncTreeUpload(sftp,"qa_data/website","/var/www/html",mode,recurse);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    printf("Directory tree uploaded.\n");

    CkSFtp_Disconnect(sftp);


    CkSFtp_Dispose(sftp);

    }