Sample code for 30+ languages & platforms
Java

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

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;

public class ChilkatExample {

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

        Ftp2 ftp = new Ftp2();

        ftp.setHostname("ftp.example.com");
        ftp.setUsername("login");
        ftp.setPassword("password");

        // Connect and login to the FTP server.
        try {
            ftp.connect();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The ListPattern property is our directory listing filter.
        // The default value is "*", which includes everything.
        System.out.println(ftp.getListPattern());

        // Fetch the current remote directory contents by
        // calling GetDirCount
        int n = ftp.getDirCount();
        if (n < 0) {
            System.out.println(ftp.getLastErrorText());
            return;
        }

        if (n > 0) {
            // Loop over the directory contents, incrementing the count
            // each time it is NOT a directory.
            int fileCount = 0;
            for (int i = 0; i < n; i++) {
                // Is this NOT a sub-directory?
                if (!ftp.getIsDirectory(i)) {
                    fileCount = fileCount + 1;
                    // Display the filename
                    System.out.println(ftp.getFilename(i));
                }
            }

            System.out.println("Total number of files = " + fileCount);
        }

        ftp.disconnect();
    }
}