Sample code for 30+ languages & platforms
Rust

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

Rust

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

let ftp = chilkat::Ftp2::new();

ftp.set_hostname("ftp.example.com");
ftp.set_username("login");
ftp.set_password("password");

// Connect and login to the FTP server.
if ftp.connect().is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// The ListPattern property is our directory listing filter.
// The default value is "*", which includes everything.
println!("{}", ftp.list_pattern());

// Fetch the current remote directory contents by
// calling GetDirCount
let n = ftp.get_dir_count();
if n < 0 {
    println!("{}", ftp.last_error_text());
    return;
}

if n > 0 {
    // Loop over the directory contents, incrementing the count
    // each time it is NOT a directory.
    let mut file_count = 0;
    for i in 0..n {

        // Is this NOT a sub-directory?
        if !ftp.get_is_directory(i) {
            file_count = file_count + 1;
            // Display the filename
            println!("{}", ftp.get_filename(i).unwrap_or_default());
        }

    }

    println!("Total number of files = {}", file_count);
}

let _ = ftp.disconnect().is_ok();