Delete a folder?

Hi,
Depending on the user’s actions, my app may, or may not have created a folder (in the user’s location of choice) called Backups.

I need to delete this folder from within my app if it exists - however, I am using the following code, but it refuses to delete the folder?

[code]Dim fi as FolderItem = dlg.ShowModal()
Dim FinalPath As Folderitem = fi.Child(“Backups”)

// DELETE THE FOLDER (WHEREVER IT IS LOCATED) IF IT ALREADY EXISTS
If FinalPath.Exists Then FinalPath.Delete
tempSourceFolder.moveFileTo fi[/code]

Can anyone help?

Thank you all in advance.

Is the folder empty? From the LR:

Oh - no, it has files inside it :frowning:
Thanks for pointing that out.

shell and
rm -R foldername

Thanks Axel.
How do I set the path to the folder I need removed, via a shell command?

This is what I have so far, but I need to somehow add the variable FinalPath to the shell command:

Dim Sh as New Shell Dim cmd As String = rm -R Backups Sh.Execute(cmd)

Or is the code below correct?

Dim Sh as New Shell Dim cmd As String = rm -R FinalPath Sh.Execute(cmd)

Or even this?

Dim Sh as New Shell Dim cmd As String = rm -R (FinalPath) Sh.Execute(cmd)

Thanks.

The LR also has an example of deleting a folder whether it is empty or not.

[code]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 folder‘s 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
// We‘re 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[/code]

Dim Sh as New Shell
Dim cmd As String = "rm -r " + FinalPath.ShellPath
Sh.Execute(cmd)

Be careful though. Make sure FinalPath IS the directory you want to delete.

Don’t know if this works in a sandboxed app though.

Thanks everyone - it now works as expected (using Albin’s code at the moment, as it’s considerably smaller).
If anyone knows if Albin’s or Scott’s versions are sandbox safe - please let me know.

Much appreciated.

Thank you all.

[quote=192216:@Axel Schneider]shell and
rm -R foldername[/quote]
I would personally recommend against this method, in the past I had a failure and accidentally deleted the entire documents folder instead of the actual folder I was supposed to delete.

I would recommend using something similar to what’s in the language reference, or alternatively use the API for moving the folder to the trash (this way it’s not actually deleted until the user empties the trash).

Ok - so it is better that I change to the code provided above by Scott Griffitts?
I will give that a try.

Thanks.

I put the function posted above by Scott, inside a module, and then tried calling the function from within a button via the code below:

// DELETE THE FOLDER AND ALL OF IT'S CONTENTS IF IT ALREADY EXISTS DeleteEntireFolder(FinalPath)

However - I get the following error message:

(FinalPath refers to the complete path where the folder might be located)
Any ideas what I have done wrong?

thank you all in advance.

dim theResult as integer = DeleteEntireFolder(FinalPath) if theResult <> 0 then 'handle error here end if

From memory…

Duhhhh - that was it!
I simply made a global property called returnCode, then changed my code to:

// DELETE THE FOLDER AND ALL OF IT'S CONTENTS IF IT ALREADY EXISTS returnCode = DeleteEntireFolder(FinalPath)

Thank you Beatrix - much appreciated

I have used the code in the LR that Scott listed above. When I run it, it does not delete all of the contents of my app folder on Mac. Deletes some of the Folders out but leaves others untouched. I am simply trying to delete an the old version of my app after updating to a new version. Just need to delete an app. Any ideas why it would not delete the entire app?

What files aren’t deleted and do you get an error message?

You might want to open a new topic with more information.