I have written a script that run in WingFTP Server which has a Lua scripting engine built in. The script recursively deletes empty folders except the specified root folder.
--Walks a file tree and delete empty folders except the root folder
function EmptyFolderDeleter(options)
local rootPath = options.path
local path = options.path
function walk(path)
local currentPath = path
--cycle through the current directory, walking directories
for dir in c_GetDir(currentPath) do
local fullPath = currentPath..dir.."/"
walk(fullPath)
end
--if we've got this far then we're at the bottom of the tree
--there are no more directories to visit
--now check if there is ANYTHING (file or folder) in the current folder
local empty = true
for isdir, file in c_GetFileDir(currentPath) do
empty = false
end
--now check the 'empty' flag and if the folder is the root
if empty == true and currentPath ~= rootPath then
c_RemoveFileDir(currentPath)
end
end
walk(path)
end
The script uses three custom Lua functions that are built into WingFTP:
c_GetDir() = an iterator that returns directory names as string
c_GetFileDir() = an iterator that returns directory and file names as strings. Also returns a boolean whether the returned string is directory or not.
c_RemoveFileDir() = Deletes the specified file or folder
My script can be called like so like so:
local root = "D:/Clients/"
for dir in c_GetDir(root) do
EmptyFolderDeleter{path=root..dir.."/"}
end
Is there is simpler or more efficient way to recursively delete empty sub folders in Lua?
Is there anything I can do to improve code readability?