Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

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

local ftp = chilkat.Ftp2.new()

ftp.hostname = "ftp.example.com"
ftp.username = "my_login"
ftp.password = "my_password"
ftp.port = 21
ftp.auth_tls = true

local success = ftp:connect()
if not success then
    print(ftp.last_error_text)
    return
end

-- Change to the "images" sub-directory located under our FTP account's home directory.
success = ftp:change_remote_dir("images")
if not success then
    print(ftp.last_error_text)
    return
end

ftp.list_pattern = "*.png"

-- Fetch the current remote directory contents by calling GetDirCount
local n = ftp:get_dir_count()
if n < 0 then
    print(ftp.last_error_text)
    return
end

local i = 0
local filename = ""
local sbLocalPath = chilkat.StringBuilder.new()

while i < n do
    filename = ftp:get_filename(i)
    print(filename)

    -- Download this file.
    sbLocalPath:set_string("qa_output/")
    sbLocalPath:append(filename)
    success = ftp:get_file(filename, sbLocalPath:get_as_string())
    if not success then
        print(ftp.last_error_text)
        return
    end

    i = i + 1
end