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

Clear the FTP Directory Listing Cache

See more FTP Examples

Demonstrates the Chilkat Ftp2.ClearDirCache method, which clears the cached listing for the current remote directory. It takes no arguments; the next indexed-listing call fetches a fresh listing from the server.

Background: The index-based directory API caches the listing the first time you read it, so repeated accessor calls avoid extra round trips. The trade-off is staleness: if the directory changes — files added or removed, whether by your own operations or another process — the cache still reflects the old state. ClearDirCache discards it so the next GetDirCount queries the server again.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkFtp2W.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Demonstrates the Ftp2.ClearDirCache method, which clears the cached listing for the current
    //  remote directory.  It takes no arguments and is a void method.

    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 first GetDirCount caches the listing.
    int n = ftp.GetDirCount();
    wprintf(L"Entries: %d\n",n);

    //  ... the remote directory changes (files added or removed by another process) ...

    //  Discard the cache so the next listing call fetches fresh data from the server.
    ftp.ClearDirCache();

    n = ftp.GetDirCount();
    wprintf(L"Entries after refresh: %d\n",n);

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