Move a FolderItem to the trash

From REALbasicWiki

Jump to: navigation, search
Overall article skill Skill ranges from beginner (green) to expert (red)

Moving a FolderItem to the trash is a bit trickier than just f.MoveFileTo TrashFolder because:

  • There are several trashes (at least one per volume)
  • ...but usually not on network volumes
  • There may already be an item with the same name in the trash
Function MoveToTrash( theItem as FolderItem ) as integer
//We will return an error code if any

dim theTrash as FolderItem


//1. Let‘s get a reference to the trash on the same volume as theItem
theTrash = theItem.TrashFolder
if theTrash=nil or (not theTrash.IsWriteable) then
return 1000 //An error code. Choose the one you want
end if

//2. Do we need to rename the item when moving it ?
//Let‘s first try the same name as theItem
g = theTrash.Child( theItem.Name )
if g<>nil and (not g.exists) then //OK, we can just move it
theItem.MoveFileTo g

else //We need to rename the file in the trash
dim idx as integer = 0

do
idx = idx + 1
g = theTrash.Child( theItem.NameWithoutExtension + " - " + Str( idx ) + "." + theItem.Extension )
loop until g<>nil and (not g.exists)

theItem.MoveFileTo g
end if

End Function

For the NameWithoutExtension method, see Split name and extension of a FolderItem.

Personal tools
related