4
\$\begingroup\$

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?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked May 8, 2015 at 16:57
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Since you're not updating the variables 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 than yours!

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 )
 end
answered May 10, 2015 at 12:20
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Regarding c_GetFileDir in the help pages: Yes I was scratching my head for ages and then I contacted WingFTP directly who confirmed that their help pages are in fact incorrect! \$\endgroup\$ Commented May 11, 2015 at 14:49
  • \$\begingroup\$ Thanks for your tips, I feel the readability of the code has been improved. Just one note: I like the the white spacing in the function parameters, e.g. walk( root ) I have not seen this before and am surprised that I quite like it! How do you format functions with multiple parameters? \$\endgroup\$ Commented May 12, 2015 at 7:29
  • 1
    \$\begingroup\$ @chazjn f( a, b, {1, 2, 3}, c, d ) \$\endgroup\$ Commented May 12, 2015 at 11:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.