Guest User

"empty_recycle_bin.pyw" - 张赐荣

a guest
Apr 27th, 2026
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | Source Code | 0 0
  1. # 作者: 张赐荣
  2. # 平台: Windows
  3.  
  4. import ctypes
  5. from ctypes import wintypes
  6.  
  7. # <-定义结构体 ->
  8. # SHQUERYRBINFO 结构体用于接收回收站信息
  9. # 文档: https://learn.microsoft.com/zh-cn/windows/win32/api/shellapi/ns-shellapi-shqueryrbinfo
  10. class SHQUERYRBINFO(ctypes.Structure):
  11.     _fields_ = [
  12.         ("cbSize", wintypes.DWORD),      # 结构体大小
  13.         ("i64Size", ctypes.c_longlong),  # 回收站内文件总大小 (字节)
  14.         ("i64NumItems", ctypes.c_longlong) # 回收站内项目总数
  15.     ]
  16.  
  17. # <- 定义常量 ->
  18. # 文档: https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-messagebox
  19. MB_OK = 0x00000000
  20. MB_ICONINFORMATION = 0x00000040
  21. MB_ICONWARNING = 0x00000030
  22. MB_ICONERROR = 0x00000010
  23.  
  24. def get_recycle_bin_count():
  25.     """获取所有驱动器回收站的文件总数"""
  26.     shell32 = ctypes.WinDLL('shell32', use_last_error=True)
  27.    
  28.     # 定义 SHQueryRecycleBinW
  29.     # 文档: https://learn.microsoft.com/zh-cn/windows/win32/api/shellapi/nf-shellapi-shqueryrecyclebinw
  30.     # HRESULT SHQueryRecycleBinW(LPCWSTR pszRootPath, LPSHQUERYRBINFO pSHQueryRBINFO);
  31.     shell32.SHQueryRecycleBinW.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(SHQUERYRBINFO)]
  32.     shell32.SHQueryRecycleBinW.restype = ctypes.HRESULT
  33.  
  34.     rb_info = SHQUERYRBINFO()
  35.     rb_info.cbSize = ctypes.sizeof(SHQUERYRBINFO)
  36.    
  37.     # pszRootPath 为 None 表示查询所有驱动器
  38.     hr = shell32.SHQueryRecycleBinW(None, ctypes.byref(rb_info))
  39.    
  40.     if hr == 0:
  41.         return rb_info.i64NumItems
  42.     return -1
  43.  
  44. def empty_recycle_bin():
  45.     shell32 = ctypes.WinDLL('shell32', use_last_error=True)
  46.     user32 = ctypes.WinDLL('user32', use_last_error=True)
  47.  
  48.     # 定义 SHEmptyRecycleBinW
  49.     # 文档: https://learn.microsoft.com/zh-cn/windows/win32/api/shellapi/nf-shellapi-shemptyrecyclebinw
  50.     shell32.SHEmptyRecycleBinW.argtypes = [wintypes.HWND, wintypes.LPCWSTR, wintypes.DWORD]
  51.     shell32.SHEmptyRecycleBinW.restype = ctypes.HRESULT
  52.  
  53.     # 记录执行前的项目数
  54.     count_before = get_recycle_bin_count()
  55.     if count_before == 0:
  56.         user32.MessageBoxW(None, "回收站已经是空的,无需操作。", "提示", MB_OK | MB_ICONINFORMATION)
  57.         return
  58.  
  59.     # 调用 API 执行清空 (dwFlags=0, 显示系统确认对话框)
  60.     hr = shell32.SHEmptyRecycleBinW(None, None, 0)
  61.  
  62.     # 记录执行后的项目数
  63.     count_after = get_recycle_bin_count()
  64.  
  65.     # 综合判断
  66.     # 逻辑:API必须成功 AND (文件数减少了 OR 文件数变为了0)
  67.     if hr == 0:
  68.         if count_after < count_before and count_after >= 0:
  69.             user32.MessageBoxW(None, f"清空成功!\n文件数从 {count_before} 降至 {count_after}。", "成功", MB_OK | MB_ICONINFORMATION)
  70.         elif count_before != -1 and count_after == count_before:
  71.             # API 返回了成功,但数量没变,说明用户在系统对话框点了“否”
  72.             user32.MessageBoxW(None, "操作已取消或未执行删除。", "提示", MB_OK | MB_ICONWARNING)
  73.         else:
  74.             user32.MessageBoxW(None, "回收站状态未发生显著变化。", "提示", MB_OK | MB_ICONWARNING)
  75.     else:
  76.         # 如果返回了非 0 的 HRESULT
  77.         err_hex = hex(hr & 0xFFFFFFFF)
  78.         user32.MessageBoxW(None, f"操作失败。错误代码: {err_hex}", "错误", MB_OK | MB_ICONERROR)
  79.  
  80. if __name__ == "__main__":
  81.     empty_recycle_bin()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment