Sample code for 30+ languages & platforms
Android™

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 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;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkSFtp sftp = new CkSFtp();

    success = sftp.Connect("my-ssh-server.com",22);
    if (success == true) {
        success = sftp.AuthenticatePw("mySshLogin","mySshPassword");
        }

    if (success == true) {
        success = sftp.InitializeSftp();
        }

    if (success != true) {
        Log.i(TAG, 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.put_SyncMustMatch("*.eml; *.gif");

    String remoteDir = "syncDownloadTest/someDir";
    String localDir = "qa_output";

    // mode=0: Download all matching files according to SyncMustMatch
    int mode = 0;

    // do not recursively traverse the remote directory tree.
    boolean recursive = false;

    success = sftp.SyncTreeDownload(remoteDir,localDir,mode,recursive);
    if (success != true) {
        Log.i(TAG, sftp.lastErrorText());
        return;
        }

    Log.i(TAG, "Success.");

  }

  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."
  }
}