Sample code for 30+ languages & platforms
Lua

Get FTP Directory Listing as XML

See more FTP Examples

Demonstrates how to call GetXmlDirListing and parse the results.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes Chilkat Ftp2 to have been previously unlocked.
-- See Unlock Ftp2 for sample code.

local ftp = chilkat.Ftp2.new()

ftp.hostname = "www.my-ftp-server.com"
ftp.username = "mFtpLogin"
ftp.password = "myFtpPassword"

-- Connect to the FTP server.
local success = ftp:connect_only()
if not success then
    print(ftp.last_error_text)
    return
end

-- Authenticate with the FTP server.
success = ftp:login_after_connect_only()
if not success then
    print(ftp.last_error_text)
    return
end

-- Retrieve (in XML format) the HOME directory of this FTP account.
local xmlListing = ftp:get_xml_dir_listing("*.*")
if not ftp.last_method_success then
    print(ftp.last_error_text)
    return
end

-- Now load the XML and parse it..
local xml = chilkat.Xml.new()
xml:load_xml(xmlListing)
print(xml:get_xml())

-- Iterate over the XML...
local i = 0
local numEntries = xml.num_children
while i < numEntries do
    local xEntry = xml:get_child(i)
    if xEntry:tag_equals("dir") then
        print("Directory: " .. xEntry.content)
    else
        local sz = xEntry:get_child_int_value("size")
        print("File: " .. xEntry:get_child_content("name") .. ", size: " .. sz)
        local xLastMod = xEntry:find_child("lastModTime")
        if xEntry.last_method_success then
            local month = xLastMod:get_attr_value_int("m")
            local year = xLastMod:get_attr_value_int("y")
            local day = xLastMod:get_attr_value_int("d")
            print("    YYYY-MM-DD: " .. year .. "-" .. month .. "-" .. day)
        end
    end

    i = i + 1
end

ftp:disconnect()

print("Success.")

-- Sample XML directory listing:
-- <?xml version="1.0" encoding="utf-8" ?>
-- <remoteDir>
--     <dir>Desktop</dir>
--     <dir>Documents</dir>
--     <dir>Downloads</dir>
--     <dir>Music</dir>
--     <dir>Pictures</dir>
--     <dir>Public</dir>
--     <dir>Templates</dir>
--     <dir>Videos</dir>
--     <file>
--         <name>c.py</name>
--         <size>1244</size>
--         <lastModTime full="20151009-000000" y="2015" d="9" m="10" hh="0" mm="0" ss="0" />
--     </file>
--     <file>
--         <name>cacerts_linux</name>
--         <size>177207</size>
--         <lastModTime full="20140915-000000" y="2014" d="15" m="9" hh="0" mm="0" ss="0" />
--     </file>
--     <file>
--         <name>empty.txt</name>
--         <size>0</size>
--         <lastModTime full="20150917-000000" y="2015" d="17" m="9" hh="0" mm="0" ss="0" />
--     </file>
--     <file>
--         <name>hamlet.xml</name>
--         <size>279658</size>
--         <lastModTime full="20160917-084100" y="2016" d="17" m="9" hh="8" mm="41" ss="0" />
--     </file>
-- </remoteDir>
--
--