Removing files

I’m trying to delete an entire folder with the following but get an io exception error is it different for folders and files? What do I have to change?

Var ASfolder As FolderItem = SpecialFolder.Documents.Child("iPuzzleData")

If ASfolder.exists = true then
  ASfolder.Remove
End if

Does it contain files?

1 Like

This code will not remove a non-empty folder. You must first loop through the folder’s contents and delete each file individually. Then you can do ASfolder.Remove.

After you create ASFolder, pass it as a parameter to a method. I will call this deleteTheFolder.

So, OTOMH:

Var ASfolder as Folderitem=SpecialFolder.Documents.Child(“iPuzzleData”)
if ASfolder.Exists then deleteTheFolder(ASFolder)

Then in your method:

Sub deleteTheFolder(myFolder as FolderItem)

For each thisItem as FolderItem in myFolder.Children

if not thisItem.IsFolder then
thisItem.Remove
else
'We found a subfolder, so we’ll call recursively
deleteTheFolder(thisItem)
end if
next

'The folder is now empty
myFolder.Remove

HTH :slight_smile:

PS: You may do this a little more simply if you don’t expect to find subfolders. Oh, and Happy New Year :smiley:

Happy New Year Jerry from Oz and thanks :beer:

1 Like