1

My goal is to restore a specific file from the Recycle Bin to a specified location, i.e. not into its original location. I found this article and also this one that explain how to enumerate files currently in the Recycle Bin. But it doesn't show how to do what I need.

Any idea how to do it?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 1
    I don't think it's possible. As far as I know, you'll need to restore it to its original location, then use (for example) MoveFile or MoveFileEx to move it to where you want it. – Jerry Coffin Mar 18 '15 at 18:27
  • 1
    @JerryCoffin: What if there's already a file in the original location? – c00000fd Mar 18 '15 at 18:35
  • @JerryCoffin: [it is possible](http://blogs.msdn.com/b/oldnewthing/archive/2011/09/01/10204404.aspx) to restore Recycle Bin files to a custom location that is different than the original location. – Remy Lebeau Mar 18 '15 at 22:56

3 Answers3

2

You can do it by simulating drag & drop. Use IShellFolder::GetUIObjectOf() to get an IDataObject interface from the recycle bin, and also to get an IDropTarget interface for your target folder. Then you can call IDropTarget::DragEnter() followed by IDropTarget::Drop() and the file will be moved to your target location.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Good idea. Thanks. I was also thinking, since I technically have a path to the file in the recycle bin (hidden) folder, can I just move it out of there with `MoveFileEx`? – c00000fd Mar 18 '15 at 19:17
  • 1
    @c00000fd Sure you can, but the recycle bin won't know you've done it, so its internal database will be wrong. – Jonathan Potter Mar 18 '15 at 19:41
  • 2
    @c00000fd: This exact scenario is actually covered by Raymond Chen in the following blog article: [Invoking commands on items in the Recycle Bin](http://blogs.msdn.com/b/oldnewthing/archive/2011/09/01/10204404.aspx). I guess you missed it when reading Raymond's other Recycle Bin articles that you linked to in your question. – Remy Lebeau Mar 18 '15 at 22:54
2

Starting from Windows Vista it is possible to use IFileOperation to restore any object from RecycleBin to any folder. Sample of usage:

procedure RestoreItem(AShellFolder: IShellFolder; AChild: PItemIDList; const ADestFolder: UnicodeString);
var
  Item: IShellItem;
  DestFolder: IShellItem;
  FileOperation: IFileOperation;
begin
  OleCheck(SHCreateItemWithParent(nil, AShellFolder, AChild, IShellItem, Item));
  try
    OleCheck(SHCreateItemFromParsingName(PWideChar(ADestFolder), nil, IShellItem, DestFolder));
    try
      OleCheck(CoCreateInstance(CLSID_FileOperation, nil, CLSCTX_ALL, IFileOperation, FileOperation));
      try
        OleCheck(FileOperation.MoveItem(Item, DestFolder, nil, nil));
        OleCheck(FileOperation.PerformOperations);
      finally
        FileOperation := nil;
      end;
    finally
      DestFolder := nil;
    end;
  finally
    Item := nil;
  end;
end;
Denis Anisimov
  • 3,297
  • 1
  • 10
  • 18
  • Thanks. I wish I saw it earlier. I went with an old Raymond Chen's code given in the different answer. – c00000fd Mar 19 '15 at 05:51
0

Drag & Drop is the way.

Then you can script it in C++, Python, CMD, PowerShell, etc...

enter image description here

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113