Sample code for 30+ languages & platforms
Rust

FTP Iterate over Files in Directory Matching ListPattern

See more RSA Examples

Uses the ListPattern property to iterate over the files in a directory matching the pattern.

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("my_login");
ftp.set_password("my_password");
ftp.set_port(21);
ftp.set_auth_tls(true);

if ftp.connect().is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// Change to the "images" sub-directory located under our FTP account's home directory.
if ftp.change_remote_dir("images").is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

ftp.set_list_pattern("*.png");

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

let mut i = 0;
let mut filename = String::new();
let sb_local_path = chilkat::StringBuilder::new();

while i < n {
    filename = ftp.get_filename(i).unwrap_or_default();
    println!("{}", filename);

    // Download this file.
    let _ = sb_local_path.set_string("qa_output/");
    let _ = sb_local_path.append(&filename);
    if ftp.get_file(&filename, &sb_local_path.get_as_string().unwrap_or_default()).is_err() {
        println!("{}", ftp.last_error_text());
        return;
    }

    i = i + 1;
}