Sample code for 30+ languages & platforms
Android™

Filter Files and Directories in an FTP Sync

See more FTP Examples

Demonstrates the synchronization filter properties SyncMustMatch, SyncMustNotMatch, SyncMustMatchDir, SyncMustNotMatchDir, and SyncCreateAllLocalDirs.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: A real sync rarely wants every file. These semicolon-separated wildcard filters shape the set: the MustMatch pairs include only matching files/directories, while the MustNotMatch pairs exclude them — letting you, say, publish only web assets while skipping build artifacts and caches. Directory filters also prune traversal, avoiding wasted time descending into folders you do not need. They apply to the tree-sync methods such as SyncLocalTree and SyncRemoteTree.

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;

    CkFtp2 ftp = new CkFtp2();

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Username("myFtpLogin");

    //  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.
    ftp.put_Password("myPassword");

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

    success = ftp.ChangeRemoteDir("public_html");
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    //  Include only files matching these semicolon-separated, case-insensitive wildcard patterns.
    ftp.put_SyncMustMatch("*.html;*.css;*.js");

    //  Exclude files matching these patterns, even if they matched SyncMustMatch.
    ftp.put_SyncMustNotMatch("*.tmp;*.bak");

    //  Restrict which directories are traversed (and which are skipped) during a tree sync.
    ftp.put_SyncMustMatchDir("assets;pages");
    ftp.put_SyncMustNotMatchDir("cache;node_modules");

    //  For a download sync, create empty remote directories on the local side too.
    ftp.put_SyncCreateAllLocalDirs(true);

    //  The filters apply to the tree sync operations, such as SyncLocalTree.
    int mode = 6;
    success = ftp.SyncLocalTree("qa_output/site",mode);
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    Log.i(TAG, "Filtered sync complete.");

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


  }

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