patternMinor
Recursively delete empty folders in Lua
Viewed 0 times
deleteemptyfoldersrecursivelylua
Problem
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.
The script uses three custom Lua functions that are built into WingFTP:
My script can be called like so like so:
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?
--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)
endThe script uses three custom Lua functions that are built into WingFTP:
c_GetDir() = an iterator that returns directory names as stringc_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 folderMy script can be called like so like so:
local root = "D:/Clients/"
for dir in c_GetDir(root) do
EmptyFolderDeleter{path=root..dir.."/"}
endIs 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?
Solution
Since you're not updating the variables
Define the function
You can also break out of your
The help pages for WFTPServer have a
different return values for
yours!
path, rootPath and currentPath anywhere, use a single variable.Define the function
walk locally.You can also break out of your
c_GetFileDir loop once you've updated the empty flag. This will remove the time to iterate through all children of a directory.The help pages for WFTPServer have a
different return values for
c_GetFileDir thanyours!
function EmptyFolderDeleter( options )
local root = options.path
local walk = function( path )
--cycle through the current directory, walking directories
for dir in c_GetDir( path ) do
walk( path..dir.."/" )
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( path ) do
empty = false
break
end
--now check the 'empty' flag and if the folder is the root
if empty and path ~= root then
c_RemoveFileDir( path )
end
end
walk( root )
endCode Snippets
function EmptyFolderDeleter( options )
local root = options.path
local walk = function( path )
--cycle through the current directory, walking directories
for dir in c_GetDir( path ) do
walk( path..dir.."/" )
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( path ) do
empty = false
break
end
--now check the 'empty' flag and if the folder is the root
if empty and path ~= root then
c_RemoveFileDir( path )
end
end
walk( root )
endContext
StackExchange Code Review Q#90182, answer score: 3
Revisions (0)
No revisions yet.