Sample code for 30+ languages & platforms
PureBasic

FTP Iterate over Files in Directory Matching ListPattern

See more RSA Examples

Uses the ListPattern property to iterate over the files in a directory matching the pattern.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"
IncludeFile "CkStringBuilder.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, "my_login")
    CkFtp2::setCkPassword(ftp, "my_password")
    CkFtp2::setCkPort(ftp, 21)
    CkFtp2::setCkAuthTls(ftp, 1)

    success = CkFtp2::ckConnect(ftp)
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ; Change to the "images" sub-directory located under our FTP account's home directory.
    success = CkFtp2::ckChangeRemoteDir(ftp,"images")
    If success <> 1
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    CkFtp2::setCkListPattern(ftp, "*.png")

    ; Fetch the current remote directory contents by calling GetDirCount
    n.i = CkFtp2::ckGetDirCount(ftp)
    If n < 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    i.i = 0
    filename.s
    sbLocalPath.i = CkStringBuilder::ckCreate()
    If sbLocalPath.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    While i < n
        filename = CkFtp2::ckGetFilename(ftp,i)
        Debug filename

        ; Download this file.
        CkStringBuilder::ckSetString(sbLocalPath,"qa_output/")
        CkStringBuilder::ckAppend(sbLocalPath,filename)
        success = CkFtp2::ckGetFile(ftp,filename,CkStringBuilder::ckGetAsString(sbLocalPath))
        If success <> 1
            Debug CkFtp2::ckLastErrorText(ftp)
            CkFtp2::ckDispose(ftp)
            CkStringBuilder::ckDispose(sbLocalPath)
            ProcedureReturn
        EndIf

        i = i + 1
    Wend


    CkFtp2::ckDispose(ftp)
    CkStringBuilder::ckDispose(sbLocalPath)


    ProcedureReturn
EndProcedure