Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = 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.

    CkSFtp sftp = new CkSFtp();

    //  Connect, authenticate, and initialize the SFTP subsystem.
    int port = 22;
    success = sftp.Connect("sftp.example.com",port);
    if (success == false) {
        Log.i(TAG, 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.
    String password = "mySshPassword";

    success = sftp.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    success = sftp.InitializeSftp();
    if (success == false) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    //  Perform a sync.
    int mode = 5;
    boolean recurse = true;
    success = sftp.SyncTreeDownload("/var/www/html","qa_output/website",mode,recurse);
    if (success == false) {
        Log.i(TAG, 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.
    CkStringTable synced = new CkStringTable();
    sftp.GetSyncedFiles(synced);

    int n = synced.get_Count();
    Log.i(TAG, "Synced " + String.valueOf(n) + " item(s):");
    int i;
    for (i = 0; i <= n - 1; i++) {
        String relPath = synced.stringAt(i);
        if (synced.get_LastMethodSuccess() == false) {
            Log.i(TAG, synced.lastErrorText());
            return;
            }

        Log.i(TAG, relPath);
        }

    sftp.Disconnect();

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}