Lua
Lua
Directory Existence Check
See more FTP Examples
How to test if a directory exists on an FTP server.A good way to check to see if a directory already exists is to try to "cd" to that remote directory by calling ChangeRemoteDir. If it succeeds, then the directory exists. If not, then it does not exist. An alternative method is to set the ListPattern = "*" and then iterate over the files/directories, looking for the directory.
Chilkat Lua Downloads
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
-- Does the "temp" directory exist?
local dirExists = ftp:change_remote_dir("/temp")
if dirExists then
print("Yes, the temp directory exists.")
-- Yes, it exists. Restore the current remote dir:
success = ftp:change_remote_dir("..")
if not success then
print(ftp.last_error_text)
return
end
end
-- Alternatively, you may set the ListPattern = "*" and
-- look for the directory:
ftp.list_pattern = "*"
local n = ftp:get_dir_count()
if n < 0 then
-- Failed to get directory listing based on ListPattern
print(ftp.last_error_text)
return
end
if n > 0 then
for i = 0, n - 1 do
local isDir = ftp:get_is_directory(i)
if isDir then
local fname = ftp:get_filename(i)
if fname == "temp" then
print("Found temp directory!")
success = ftp:disconnect()
return
end
end
end
end
success = ftp:disconnect()