Sample code for 30+ languages & platforms
Rust

Delete Local Files that Do Not Exist on the FTP Server

See more FTP Examples

Demonstrates how to get a list of local files in a directory tree that do not exist on the FTP server.

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

ftp.set_keep_session_log(true);

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

// Set the current remote directory to the root of
// the remote tree to be compared.
if ftp.change_remote_dir("abc123").is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// Recursively descend the local directory tree
// and find the files that exist locally but not remotely.
// These are the files what would be uploaded via
// the SyncRemoteTree method call with mode = 1.
// (Mode 1 would upload all files that do not exist on the FTP server.)

// The actual uploading is avoided by setting the preview-only argument to true.
let mode = 1;
let descend_tree = true;
let preview_only = true;
if ftp.sync_remote_tree2("/temp/abc123", mode, descend_tree, preview_only).is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// The files what would've been uploaded are now available in the SyncPreview property,
// which contains a list of local file paths, one per line.
// A program can iterate over them like this:
let sa = chilkat::StringArray::new();
sa.load_from_text(&ftp.sync_preview());

let fac = chilkat::FileAccess::new();

let num_files = sa.count();
let mut i = 0;
let mut local_file_path = String::new();
while i < num_files {
    local_file_path = sa.get_string(i).unwrap_or_default();
    println!("{}", local_file_path);

    // An application can delete the file using Chilkat's file access object,
    // or it can choose to use the native file API available in the programming language:
    if fac.file_delete(&local_file_path).is_err() {
        println!("Failed to delete: {}", local_file_path);
    }

    i = i + 1;
}

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