# How to trash files / directories?

Both DeleteFile and DeleteDirectory can only delete files / directories, and lack the option to move them to the trash. Is there an elegant (and OS-independent) way to trash items?

-
It's probably not going to be easy on Windows... JLink/.NETLink etc. might be your best bet –  rm -rf Aug 3 '13 at 20:22
If you are willing to solve it via Java. You can use FileUtil from incubator project.See the link github.com/gelosie/jdic/blob/master/src/incubator/fileutil/…. There is a method public abstract boolean recycle(File file) under class NativeFileUtil. –  s.s.o Aug 3 '13 at 20:52
Naked linux does not provide a "trash can", though specific linux window managers might, so it's hard to see how this can be universally OS independent. –  image_doctor Aug 4 '13 at 0:10
Thanks for all the comments! Using a 3rd party Java library or C++ code via JNI isn't exactly elegant, but if its the only way to get the job done, then so be it. I didn't realized that on Linux the trash implementation varies from one distribution to another, so indeed making this fully OS independent seems unlikely to do. –  Teake Nutma Aug 4 '13 at 7:14

At first I thought to withhold this answer because it is for OS X only. I thought I would look silly if someone posted a cross-platform solution. But it doesn't look like that's going to happen, so I feel I might as well show how it can be done on an OS that makes it easy. The only tricky bit here is that you have to be aware that the OS X Trash won't accept two files with the same name.

moveToTrash[fullPathName_String] :=
Module[{trash, fileName},
trash = FileNameJoin[{HomeDirectory[], ".Trash"}];
fileName = FileNameTake[fullPathName];
While[FileExistsQ[FileNameJoin[{trash, fileName}]],fileName = "#" <> fileName];
RenameFile[fullPathName, FileNameJoin[{trash, fileName}]]]


Can't show any results here; this code is only for its side-effects. However, you can play with using a piece of code that was one of my unit tests. The code should be evaluated a few times, say three, to verify that multiple files with the same name can be moved to Trash.

testFile =
Export[FileNameJoin[{HomeDirectory[], "Desktop", "test.txt"}], "The quick brown fox ..."];
moveToTrash[testFile];


Remember this is for OS X only.

-
Thanks anyhow! Two small comments though: this moves everything to ~/.Trash, but AFAIK OS X has different trash locations for each volume. So if you trash something that's not on the same volume as ~/, you won't get the native behaviour. Also, how about an \$OperatingSystem === "MacOSX" switch? –  Teake Nutma Aug 4 '13 at 18:49
@TeakeNutma. 1) I happen to prefer that all files get moved to my home .Trash. Since almost every file that goes into any .Trash gets deleted from the disk drive its on, normally it doesn't matter which particular volume it's on. It might matter if I were using portable disk drives, but I don't. 2) Why bother with an OS test when the code is OS X only? My warning is clear. If a Windows user chooses to try it, he/she must accept whatever consequences ensue. –  m_goldberg Aug 5 '13 at 2:07