Sample code for 30+ languages & platforms
Lua

Get Number of FIles in Directory, not including sub-directories

See more FTP Examples

_LANGUAGE_ example demonstrating how to get the number of files in a directory not including sub-directories.

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 = "login"
ftp.password = "password"

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

-- The ListPattern property is our directory listing filter.
-- The default value is "*", which includes everything.
print(ftp.list_pattern)

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

if n > 0 then
    -- Loop over the directory contents, incrementing the count
    -- each time it is NOT a directory.
    local fileCount = 0
    for i = 0, n - 1 do
        -- Is this NOT a sub-directory?
        if not ftp:get_is_directory(i) then
            fileCount = fileCount + 1
            -- Display the filename
            print(ftp:get_filename(i))
        end
    end

    print("Total number of files = " .. fileCount)
end

success = ftp:disconnect()