On 05/05/2010 20:33, Thomas Heller wrote:
Does someone have example code snippets how to walk the shell namespace in 
Python?
I'm lost in all these pidls and folders.

The code below uses the pywin32 functions to walk. Because there's all
sorts of things which can happen, I've implemented a v. broad except:
clause just to keep things going. Since this uses the pywin32 wrappers,
it's hiding a certain amount of the PIDL/SHITEM stuff, but the functions
are pretty much the underlying win32 ones.

One thing I don't know how to do without using comtypes is to find the parent.
the SHBindToParent function would be the way to go but it's not exposed and
I don't have the energy to set up all the comtypes boilerplate to make it 
happen:
I'm sure you can manage, though :)

  http://msdn.microsoft.com/en-us/library/bb762114%28VS.85%29.aspx

The upside of the pywin32 wrappers (lots of stuff done for you) is also
its downside (you can't take complete control if you need to).

<code>
import os, sys

import pythoncom
from win32com.shell import shell, shellcon

def walk (root=None, parent=None):
  try:
    if root is None:
      root_folder = shell.SHGetDesktopFolder ()
      root_name = root_folder.GetDisplayNameOf ([], shellcon.SHGDN_NORMAL)
    else:
      root_folder = parent.BindToObject (root, None, shell.IID_IShellFolder)
      root_name = parent.GetDisplayNameOf (root, shellcon.SHGDN_NORMAL)

    folders = list (root_folder.EnumObjects (None, shellcon.SHCONTF_FOLDERS))
    items = list (root_folder.EnumObjects (None, shellcon.SHCONTF_NONFOLDERS))
    yield (
      root_name,
      [root_folder.GetDisplayNameOf (f, shellcon.SHGDN_NORMAL) for f in 
folders],
      [root_folder.GetDisplayNameOf (i, shellcon.SHGDN_NORMAL) for i in items]
    )

    for folder in folders:
      for info in walk (folder, root_folder):
        yield info

  except pythoncom.com_error:
    pass

if __name__ == '__main__':
  for root, folders, items in walk ():
    print root, folders, items

</code>


TJG
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to