Sample code for 30+ languages & platforms
Rust

File Existence Check

See more FTP Examples

Testing to see if a file exists on the FTP server. The GetSizeByName method is a convenient way to check if a file exists. It will return -1 if the file does not exist, otherwise it returns the size of the file in bytes.

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("username");
ftp.set_password("password");

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

// Set the current remote directory to where the file is located:
if ftp.change_remote_dir("something").is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// Test to see if the file exists by getting the file size by name. 
// If a -1 is returned, the file does not exist.
let file_size = ftp.get_size_by_name("test123.txt");
if file_size < 0 {
    println!("file does not exist");
} else {
    println!("file exists and is {} bytes in size", file_size);
}

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