Sample code for 30+ languages & platforms
Rust

Get a Directory Listing as XML

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetXmlDirListing method, which retrieves a directory listing and returns the parsed entries as an XML document rooted at <remoteDir>, with a <file> element per entry. The only argument is the listing pattern; an empty string lists the current directory.

Tip: Code to parse the returned XML can be generated at Chilkat Tools.

Background: This is the parse-friendly listing: Chilkat has already interpreted the server's raw LIST output — whatever its format — into consistent XML with names, sizes, dates, and types as elements. That spares you from writing format-specific parsers and is the right choice when a program, rather than a person, consumes the listing. Load the result into a Chilkat.Xml object to iterate the entries.

Chilkat Rust Downloads

Rust

// Demonstrates the Ftp2.GetXmlDirListing method, which returns a directory listing as a parsed
// XML document.  The only argument is the listing pattern; an empty string lists the current
// remote directory.

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

ftp.set_hostname("ftp.example.com");
ftp.set_username("myFtpLogin");

// Normally you would not hard-code the password in source.  You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
ftp.set_password("myPassword");

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

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

// The XML is rooted at <remoteDir>, with <file> elements for each entry.  This is easier to
// parse programmatically than the raw text listing.
let Ok(xml) = ftp.get_xml_dir_listing("*.html") else {
    println!("{}", ftp.last_error_text());
    return;
};

println!("{}", xml);

// The XML can be loaded into a Chilkat.Xml object for further processing.

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