Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { Ftp2, StringBuilder } from '@chilkat/react-native'

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

  const ftp = new Ftp2();

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

  try {
    await ftp.connectAsync();
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // Change to the "images" sub-directory located under our FTP account's home directory.
  try {
    await ftp.changeRemoteDirAsync('images');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  ftp.listPattern = '*.png';

  // Fetch the current remote directory contents by calling GetDirCount
  const n = await ftp.getDirCountAsync();
  if (n < 0) {
    console.log(ftp.lastErrorText);
    return;
  }

  let i = 0;
  let filename = '';
  const sbLocalPath = new StringBuilder();

  while (i < n) {
    filename = await ftp.getFilenameAsync(i);
    console.log(filename);

    // Download this file.
    sbLocalPath.setString('qa_output/');
    sbLocalPath.append(filename);
    try {
      await ftp.getFileAsync(filename, sbLocalPath.getAsString());
    } catch {
      console.log(ftp.lastErrorText);
      return;
    }

    i++;
  }
}