Recycle Bin

List all deleted files

List all the versions of all the files which are in the Recycle Bin.

import winshell

all_deleted_files = list(winshell.recycle_bin())
print(all_deleted_files)

Discussion

The recycle_bin() factory function returns a ShellRecycleBin object which represents the union of all drive-specific recycle bins on the system. (It’s not possible AFAICT to select only one). This objects is iterable, yielding one ShellRecycledItem for each version of each file contained in the bin.

If you just need to perform some action over each files (as opposed to bringing them all together into an in-memory structure) then iterate over the recycle bin.

If you just want the versions of some specific original file, you want the ShellRecycleBin.versions() function.

Undelete by criteria

Undelete only those .txt files which were deleted today.

import os
import datetime
import fnmatch
import winshell

midnight = datetime.datetime.today().replace(hour=0, minute=0, second=0)
for item in winshell.recycle_bin():
    if fnmatch.fnmatch(os.path.basename(item.original_filename()), "*.txt"):
        if item.recycle_date() >= midnight:
            print("About to undelete %r" % item)

Discussion

Iterating over a ShellRecycleBin yields ShellRecycledItem objects in no particular order, each one representing a file deleted from a particular path at a particular time. These objects expose ShellRecycledItem.original_filename() and ShellRecycledItem.recycle_date() representing the name of the file when it was deleted and the timestamp of that event.

It’s possible for a file at the same path to be deleted multiple times before the recycle bin is emptied. When undeleting these files a copy is generated in the original folder based on the original name.