walk_shell_folders.py 739 B

123456789101112131415161718192021222324
  1. # A little sample that walks from the desktop into child
  2. # items.
  3. from win32com.shell import shell, shellcon
  4. def walk(folder, depth=2, indent=""):
  5. try:
  6. pidls = folder.EnumObjects(0, shellcon.SHCONTF_FOLDERS)
  7. except shell.error:
  8. # no items
  9. return
  10. for pidl in pidls:
  11. dn = folder.GetDisplayNameOf(pidl,
  12. shellcon.SHGDN_NORMAL)
  13. print indent, dn
  14. if depth:
  15. try:
  16. child = folder.BindToObject(pidl, None,
  17. shell.IID_IShellFolder)
  18. except shell.error:
  19. pass
  20. else:
  21. walk(child, depth-1, indent+" ")
  22. walk(shell.SHGetDesktopFolder())