Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oFtp
LOCAL n
LOCAL i
LOCAL cFilename
LOCAL oSbLocalPath

nSuccess := 0

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

oFtp := CreateObject("Chilkat.Ftp2")

oFtp:Hostname := "ftp.example.com"
oFtp:Username := "my_login"
oFtp:Password := "my_password"
oFtp:Port := 21
oFtp:AuthTls := 1

nSuccess := oFtp:Connect()
IF (nSuccess != 1)
    ? oFtp:LastErrorText
    oFtp:destroy()
    RETURN
ENDIF

//  Change to the "images" sub-directory located under our FTP account's home directory.
nSuccess := oFtp:ChangeRemoteDir("images")
IF (nSuccess != 1)
    ? oFtp:LastErrorText
    oFtp:destroy()
    RETURN
ENDIF

oFtp:ListPattern := "*.png"

//  Fetch the current remote directory contents by calling GetDirCount
n := oFtp:GetDirCount()
IF (n < 0)
    ? oFtp:LastErrorText
    oFtp:destroy()
    RETURN
ENDIF

i := 0

oSbLocalPath := CreateObject("Chilkat.StringBuilder")

DO WHILE i < n
    cFilename := oFtp:GetFilename(i)
    ? cFilename

    //  Download this file.
    oSbLocalPath:SetString("qa_output/")
    oSbLocalPath:Append(cFilename)
    nSuccess := oFtp:GetFile(cFilename, oSbLocalPath:GetAsString())
    IF (nSuccess != 1)
        ? oFtp:LastErrorText
        oFtp:destroy()
        oSbLocalPath:destroy()
        RETURN
    ENDIF

    i := i + 1
ENDDO

oFtp:destroy()
oSbLocalPath:destroy()