Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkFtp2W.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  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.

    CkFtp2W ftp;

    ftp.put_Hostname(L"ftp.example.com");
    ftp.put_Username(L"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.put_Password(L"myPassword");

    success = ftp.Connect();
    if (success == false) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }

    success = ftp.ChangeRemoteDir(L"public_html");
    if (success == false) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }

    //  The XML is rooted at <remoteDir>, with <file> elements for each entry.  This is easier to
    //  parse programmatically than the raw text listing.
    const wchar_t *xml = ftp.getXmlDirListing(L"*.html");
    if (ftp.get_LastMethodSuccess() == false) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }

    wprintf(L"%s\n",xml);

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

    success = ftp.Disconnect();
    if (success == false) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }
    }