Last active
July 8, 2016 15:19
-
-
Save CaptainPRICE/7a459de4f101a3e48e63ddff1ba0c1f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Localization of used libraries/functions, as an access to local (upvalues) is faster than global. | |
local file = file | |
local file_Delete, file_Find = file.Delete, file.Find | |
local file_DeleteDir -- Separate declaration from definition (since it is a recursive function). | |
--- A function to recursively remove/delete files and sub-directories. | |
--- gate_folder_name: Name of the folder for the Editor mode (e.g. expression2). | |
--- selected_folder_path: Directory (relative to gate_folder_name) to be deleted/removed. | |
file_DeleteDir = function(gate_folder_name, selected_folder_path) | |
if not isstring(gate_folder_name) or not isstring(selected_folder_path) or gate_folder_name:len() < 1 or selected_folder_path:len() < 1 or gate_folder_name == "/" or selected_folder_path == "/" then return end -- Avoid errors and removing/deleting everything from gate's folder (don't display an option on the root/gate folder). | |
local path = ((gate_folder_name:sub(-1) == "/" and gate_folder_name) or (gate_folder_name .. "/")) .. ((selected_folder_path:sub(-1) == "/" and selected_folder_path) or (selected_folder_path .. "/")) -- Make sure both arguments end with '/' character. | |
local found_files, found_directories = file_Find(path .. "*", "DATA") -- Search for files and folders in selected path. | |
for _, file_name in next, found_files do | |
if file_name:sub(-4):upper() == ".TXT" then -- Security measure, only process files with '.txt' (text file) extension. | |
file_Delete(path .. file_name) -- First, remove/delete files from directory (it is automatically relative to DATA folder). | |
end | |
end | |
for _, directory_name in next, found_directories do | |
if directory_name ~= '..' then -- Another security measure (skip the hardlink to the parent directory). | |
file_DeleteDir(gate_folder_name, selected_folder_path .. "/" .. directory_name) -- Second, call this function recursively to remove/delete all files and sub-directories. | |
end | |
end | |
return file_Delete(path:sub(1, -2)) -- Finally, try to remove/delete (empty) directory. | |
end | |
file_DeleteDir("expression2", "path/to/lib") -- Given the correct path. In this case, it would remove/delete all files and sub-directories from "expression2/path/to/lib" directory. | |
file_DeleteDir("expression2/", "path/to/lib/") -- Same as above, it shows that the function will still work even with trailing '/' character (in either of the two arguments). | |
-- The following won't work (at least it is not supposed to be called like so): | |
file_DeleteDir("/", "") | |
file_DeleteDir() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment