Sample code for 30+ languages & platforms
PureBasic

Delete Local Files that Do Not Exist on the FTP Server

See more FTP Examples

Demonstrates how to get a list of local files in a directory tree that do not exist on the FTP server.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkStringArray.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

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

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkUsername(ftp, "login")
    CkFtp2::setCkPassword(ftp, "password")

    CkFtp2::setCkKeepSessionLog(ftp, 1)

    ; Connect and login to the FTP server.
    success = CkFtp2::ckConnect(ftp)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; Set the current remote directory to the root of
    ; the remote tree to be compared.
    success = CkFtp2::ckChangeRemoteDir(ftp,"abc123")
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; Recursively descend the local directory tree
    ; and find the files that exist locally but not remotely.
    ; These are the files what would be uploaded via
    ; the SyncRemoteTree method call with mode = 1.
    ; (Mode 1 would upload all files that do not exist on the FTP server.)

    ; The actual uploading is avoided by setting the preview-only argument to 1.
    mode.i = 1
    descendTree.i = 1
    previewOnly.i = 1
    success = CkFtp2::ckSyncRemoteTree2(ftp,"/temp/abc123",mode,descendTree,previewOnly)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; The files what would've been uploaded are now available in the SyncPreview property,
    ; which contains a list of local file paths, one per line.
    ; A program can iterate over them like this:
    sa.i = CkStringArray::ckCreate()
    If sa.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringArray::ckLoadFromText(sa,CkFtp2::ckSyncPreview(ftp))

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

    numFiles.i = CkStringArray::ckCount(sa)
    i.i = 0
    localFilePath.s
    While (i < numFiles)
        localFilePath = CkStringArray::ckGetString(sa,i)
        Debug localFilePath

        ; An application can delete the file using Chilkat's file access object,
        ; or it can choose to use the native file API available in the programming language:
        success = CkFileAccess::ckFileDelete(fac,localFilePath)
        If success <> 1
            Debug "Failed to delete: " + localFilePath
        EndIf

        i = i + 1
    Wend

    success = CkFtp2::ckDisconnect(ftp)


    CkFtp2::ckDispose(ftp)
    CkStringArray::ckDispose(sa)
    CkFileAccess::ckDispose(fac)


    ProcedureReturn
EndProcedure