Sample code for 30+ languages & platforms
React Native

Get Number of FIles in Directory, not including sub-directories

See more FTP Examples

_LANGUAGE_ example demonstrating how to get the number of files in a directory not including sub-directories.

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);

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

  if (n > 0) {
    // Loop over the directory contents, incrementing the count
    // each time it is NOT a directory.
    let fileCount = 0;
    for (let i = 0; i < n; i++) {

      // Is this NOT a sub-directory?
      if (!await ftp.getIsDirectoryAsync(i)) {
        fileCount++;
        // Display the filename
        console.log(await ftp.getFilenameAsync(i));
      }
    }

    console.log(`Total number of files = ${fileCount}`);
  }

  await ftp.disconnectAsync();
}