In my project, I’m trying to delete a folder, including all of its subdirectories and subfiles. I found some previous code in the Corona Forums, which made use of LFS and os.remove(). However, for some reason, the code can only delete the subfiles, but NOT the subdirectories. For the subdirectories, the reason that Corona gives for failure is PERMISSION DENIED. I am at a loss at why the code is deleting only the files, but not the subfolders?
Here’s the code: The code assumes that there is a folder in the Documents directory called folder1. Under folder1, I just added a bunch of subfolders and subfiles.
local title = display.newText("DELETE", display.contentCenterX, display.contentCenterY - 100 + 15, native.systemFont, 20 ) local function deleteDirectory(dir) local resultOK, errorMsg; --remove files from directory for file in lfs.dir(dir) do if ((file == ".") or (file == "..")) then -- skip system files -- do nothing else local theFile = dir.."/"..file; if (lfs.attributes(theFile, "mode") == "directory") then deleteDirectory(theFile); else resultOK, errorMsg = os.remove(theFile); if (not resultOK) then print("Error removing file: "..file..":"..errorMsg); else print("Removed file: " .. file) end end end end -- remove directory resultOK, errorMsg = os.remove(dir); if (not resultOK) then print("Error removing directory: "..dir.."---\>\>\>"..errorMsg); else print("Removed directory: " .. dir) end end local function delete() deleteDirectory(system.pathForFile("folder1", system.DocumentsDirectory)) end title:addEventListener("tap", delete)
Edit: The code is copy & pastable