Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ftp.i = CkFtp2::ckCreate()
    If ftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkUsername(ftp, "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.
    CkFtp2::setCkPassword(ftp, "myPassword")

    success = CkFtp2::ckConnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    success = CkFtp2::ckChangeRemoteDir(ftp,"public_html")
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ;  The first GetDirCount caches the listing.
    n.i = CkFtp2::ckGetDirCount(ftp)
    Debug "Entries: " + Str(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.
    CkFtp2::ckClearDirCache(ftp)

    n = CkFtp2::ckGetDirCount(ftp)
    Debug "Entries after refresh: " + Str(n)

    success = CkFtp2::ckDisconnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf



    CkFtp2::ckDispose(ftp)


    ProcedureReturn
EndProcedure