Sample code for 30+ languages & platforms
Rust

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

// To get file and sub-directory information, simply
// loop from 0 to ftp.GetDirCount() - 1
let mut n: i32 = 0;
n = ftp.get_dir_count();
if n < 0 {
    println!("{}", ftp.last_error_text());
    return;
}

if n > 0 {
    for i in 0..n {

        // Display the filename
        println!("{}", ftp.get_filename(i).unwrap_or_default());

        // Display the file size (in bytes)
        println!("{}", ftp.get_size(i));

        // Is this a sub-directory?
        if ftp.get_is_directory(i) {
            println!(".. this is a sub-directory");
        }

        println!("--");
    }

}

println!("-----------------------------------");

// Only files and directories
// matching the ListPattern are returned.
ftp.set_list_pattern("*.asp");
n = ftp.get_dir_count();
if n < 0 {
    println!("{}", ftp.last_error_text());
    return;
}

if n > 0 {
    for i in 0..n {

        // Display the filename
        println!("{}", ftp.get_filename(i).unwrap_or_default());

        // Display the file size (in bytes)
        println!("{}", ftp.get_size(i));

        println!("--");
    }

}

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