Sample code for 30+ languages & platforms
React Native

Get FTP Directory Listing Information

See more FTP Examples

_LANGUAGE_ example showing how to get information about files and subdirectories in the current remote FTP directory.

Chilkat React Native Downloads

React Native
import { Ftp2 } 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 = 'login';
  ftp.password = 'password';

  // Connect and login to the FTP server.
  try {
    await ftp.connectAsync();
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // The ListPattern property is our directory listing filter.
  // The default value is "*", which includes everything.
  console.log(ftp.listPattern);

  // To get file and sub-directory information, simply
  // loop from 0 to ftp.GetDirCount() - 1
  let n = 0;
  n = await ftp.getDirCountAsync();
  if (n < 0) {
    console.log(ftp.lastErrorText);
    return;
  }

  if (n > 0) {
    for (let i = 0; i < n; i++) {

      // Display the filename
      console.log(await ftp.getFilenameAsync(i));

      // Display the file size (in bytes)
      console.log(await ftp.getSizeAsync(i));

      // Is this a sub-directory?
      if (await ftp.getIsDirectoryAsync(i)) {
        console.log('.. this is a sub-directory');
      }

      console.log('--');
    }
  }

  console.log('-----------------------------------');

  // Only files and directories
  // matching the ListPattern are returned.
  ftp.listPattern = '*.asp';
  n = await ftp.getDirCountAsync();
  if (n < 0) {
    console.log(ftp.lastErrorText);
    return;
  }

  if (n > 0) {
    for (let i = 0; i < n; i++) {

      // Display the filename
      console.log(await ftp.getFilenameAsync(i));

      // Display the file size (in bytes)
      console.log(await ftp.getSizeAsync(i));

      console.log('--');
    }
  }

  await ftp.disconnectAsync();
}