I want to have a tmp folder for each instance of my application running on a server.
When my application finishes I want to clean up that folder and any subdirectories it create under that folder + files within those.
Suggestions… folder.delete might work if the folder has no children… but I need to be thorough and clean recursively from my tmp folder down…
There’s an example in the LR that seems to do what you want
http://documentation.xojo.com/index.php/FolderItem.Delete
Function DeleteEntireFolder(theFolder as FolderItem, continueIfErrors as Boolean = false) As Integer
// Returns an error code if it fails, or zero if the folder was deleted successfully
dim returnCode, lastErr, itemCount as integer
dim files(), dirs() as FolderItem
if theFolder = nil or not theFolder.Exists() then
return 0
end if
// Collect the folders contents first.
// This is faster than collecting them in reverse order and deleting them right away!
itemCount = theFolder.Count
for i as integer = 1 to itemCount
dim f as FolderItem
f = theFolder.TrueItem( i )
if f <> nil then
if f.Directory then
dirs.Append f
else
files.Append f
end if
end if
next
// Now delete the files
for each f as FolderItem in files
f.Delete
lastErr = f.LastErrorCode // Check if an error occurred
if lastErr <> 0 then
if continueIfErrors then
if returnCode = 0 then returnCode = lastErr
else
// Return the error code if any. This will cancel the deletion.
return lastErr
end if
end if
next
redim files(-1) // free the memory used by the files array before we enter recursion
// Now delete the directories
for each f as FolderItem in dirs
lastErr = DeleteEntireFolder( f, continueIfErrors )
if lastErr <> 0 then
if continueIfErrors then
if returnCode = 0 then returnCode = lastErr
else
// Return the error code if any. This will cancel the deletion.
return lastErr
end if
end if
next
if returnCode = 0 then
// Were done without error, so the folder should be empty and we can delete it.
theFolder.Delete
returnCode = theFolder.LastErrorCode
end if
return returnCode
End Function
Boy, that was timely! I was just starting to implement a recursive folder deletion routine for an app I’m writing. You just saved me at least a couple of hours coding and testing.