Sample code for 30+ languages & platforms
Dart

FTP Iterate over Files in Directory Matching ListPattern

See more RSA Examples

Uses the ListPattern property to iterate over the files in a directory matching the pattern.

Chilkat Dart Downloads

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

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

  final ftp = CkFtp2();

  ftp.hostname = 'ftp.example.com';
  ftp.username = 'my_login';
  ftp.password = 'my_password';
  ftp.port = 21;
  ftp.authTls = true;

  try {
    ftp.connect();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Change to the "images" sub-directory located under our FTP account's home directory.
  try {
    ftp.changeRemoteDir('images');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  ftp.listPattern = '*.png';

  // Fetch the current remote directory contents by calling GetDirCount
  final n = ftp.getDirCount();
  if (n < 0) {
    print(ftp.lastErrorText);
    return;
  }

  var i = 0;
  var filename = '';
  final sbLocalPath = CkStringBuilder();

  while (i < n) {
    filename = ftp.getFilename(i);
    print(filename);

    // Download this file.
    sbLocalPath.setString('qa_output/');
    sbLocalPath.append(filename);
    try {
      ftp.getFile(filename, sbLocalPath.getAsString());
    } on ChilkatException catch (e) {
      print(e.lastErrorText);
      return;
    }

    i++;
  }
}