Lua
Lua
Delete Local Files that Do Not Exist on the FTP Server
See more FTP Examples
Demonstrates how to get a list of local files in a directory tree that do not exist on the FTP server.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"
ftp.keep_session_log = true
-- Connect and login to the FTP server.
local success = ftp:connect()
if not success then
print(ftp.last_error_text)
return
end
-- Set the current remote directory to the root of
-- the remote tree to be compared.
success = ftp:change_remote_dir("abc123")
if not success then
print(ftp.last_error_text)
return
end
-- Recursively descend the local directory tree
-- and find the files that exist locally but not remotely.
-- These are the files what would be uploaded via
-- the SyncRemoteTree method call with mode = 1.
-- (Mode 1 would upload all files that do not exist on the FTP server.)
-- The actual uploading is avoided by setting the preview-only argument to true.
local mode = 1
local descendTree = true
local previewOnly = true
success = ftp:sync_remote_tree2("/temp/abc123", mode, descendTree, previewOnly)
if not success then
print(ftp.last_error_text)
return
end
-- The files what would've been uploaded are now available in the SyncPreview property,
-- which contains a list of local file paths, one per line.
-- A program can iterate over them like this:
local sa = chilkat.StringArray.new()
sa:load_from_text(ftp.sync_preview)
local fac = chilkat.FileAccess.new()
local numFiles = sa.count
local i = 0
local localFilePath = ""
while i < numFiles do
localFilePath = sa:get_string(i)
print(localFilePath)
-- An application can delete the file using Chilkat's file access object,
-- or it can choose to use the native file API available in the programming language:
success = fac:file_delete(localFilePath)
if not success then
print("Failed to delete: " .. localFilePath)
end
i = i + 1
end
success = ftp:disconnect()