Sample code for 30+ languages & platforms
Unicode C

Get FTP Directory Listing as XML

See more FTP Examples

Demonstrates how to call GetXmlDirListing and parse the results.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkFtp2W.h>
#include <C_CkXmlW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2W ftp;
    const wchar_t *xmlListing;
    HCkXmlW xml;
    int i;
    int numEntries;
    HCkXmlW xEntry;
    int sz;
    HCkXmlW xLastMod;
    int month;
    int year;
    int day;

    success = FALSE;

    // This example assumes Chilkat Ftp2 to have been previously unlocked.
    // See Unlock Ftp2 for sample code.

    ftp = CkFtp2W_Create();

    CkFtp2W_putHostname(ftp,L"www.my-ftp-server.com");
    CkFtp2W_putUsername(ftp,L"mFtpLogin");
    CkFtp2W_putPassword(ftp,L"myFtpPassword");

    // Connect to the FTP server.
    success = CkFtp2W_ConnectOnly(ftp);
    if (success != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    // Authenticate with the FTP server.
    success = CkFtp2W_LoginAfterConnectOnly(ftp);
    if (success != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    // Retrieve (in XML format) the HOME directory of this FTP account.
    xmlListing = CkFtp2W_getXmlDirListing(ftp,L"*.*");
    if (CkFtp2W_getLastMethodSuccess(ftp) != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    // Now load the XML and parse it..
    xml = CkXmlW_Create();
    CkXmlW_LoadXml(xml,xmlListing);
    wprintf(L"%s\n",CkXmlW_getXml(xml));

    // Iterate over the XML...
    i = 0;
    numEntries = CkXmlW_getNumChildren(xml);
    while (i < numEntries) {
        xEntry = CkXmlW_GetChild(xml,i);
        if (CkXmlW_TagEquals(xEntry,L"dir") == TRUE) {
            wprintf(L"Directory: %s\n",CkXmlW_content(xEntry));
        }
        else {
            sz = CkXmlW_GetChildIntValue(xEntry,L"size");
            wprintf(L"File: %s, size: %d\n",CkXmlW_getChildContent(xEntry,L"name"),sz);
            xLastMod = CkXmlW_FindChild(xEntry,L"lastModTime");
            if (CkXmlW_getLastMethodSuccess(xEntry) == TRUE) {
                month = CkXmlW_GetAttrValueInt(xLastMod,L"m");
                year = CkXmlW_GetAttrValueInt(xLastMod,L"y");
                day = CkXmlW_GetAttrValueInt(xLastMod,L"d");
                wprintf(L"    YYYY-MM-DD: %d-%d-%d\n",year,month,day);
                CkXmlW_Dispose(xLastMod);
            }

        }

        CkXmlW_Dispose(xEntry);
        i = i + 1;
    }

    CkFtp2W_Disconnect(ftp);

    wprintf(L"Success.\n");

    // Sample XML directory listing:
    // <?xml version="1.0" encoding="utf-8" ?>
    // <remoteDir>
    //     <dir>Desktop</dir>
    //     <dir>Documents</dir>
    //     <dir>Downloads</dir>
    //     <dir>Music</dir>
    //     <dir>Pictures</dir>
    //     <dir>Public</dir>
    //     <dir>Templates</dir>
    //     <dir>Videos</dir>
    //     <file>
    //         <name>c.py</name>
    //         <size>1244</size>
    //         <lastModTime full="20151009-000000" y="2015" d="9" m="10" hh="0" mm="0" ss="0" />
    //     </file>
    //     <file>
    //         <name>cacerts_linux</name>
    //         <size>177207</size>
    //         <lastModTime full="20140915-000000" y="2014" d="15" m="9" hh="0" mm="0" ss="0" />
    //     </file>
    //     <file>
    //         <name>empty.txt</name>
    //         <size>0</size>
    //         <lastModTime full="20150917-000000" y="2015" d="17" m="9" hh="0" mm="0" ss="0" />
    //     </file>
    //     <file>
    //         <name>hamlet.xml</name>
    //         <size>279658</size>
    //         <lastModTime full="20160917-084100" y="2016" d="17" m="9" hh="8" mm="41" ss="0" />
    //     </file>
    // </remoteDir>
    // 
    // 


    CkFtp2W_Dispose(ftp);
    CkXmlW_Dispose(xml);

    }