Sample code for 30+ languages & platforms
Dart

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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

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

  final sftp = CkSFtp();

  success = true;
  try {
    sftp.connect('my-ssh-server.com', 22);
  } on ChilkatException {
    success = false;
  }
  if (success) {
    success = true;
    try {
      sftp.authenticatePw('mySshLogin', 'mySshPassword');
    } on ChilkatException {
      success = false;
    }
  }

  if (success) {
    success = true;
    try {
      sftp.initializeSftp();
    } on ChilkatException {
      success = false;
    }
  }

  if (!success) {
    print(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.syncMustMatch = '*.eml; *.gif';

  final remoteDir = 'syncDownloadTest/someDir';
  final localDir = 'qa_output';

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

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

  success = true;
  try {
    sftp.syncTreeDownload(remoteDir, localDir, mode, recursive);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print(sftp.lastErrorText);
    return;
  }

  print('Success.');
}