viewstate.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """
  2. Demonstrates how to propagate a folder's view state to all its subfolders
  3. The format of the ColInfo stream is apparently undocumented, but
  4. it can be read raw from one folder and copied to another's view state.
  5. """
  6. from win32com.shell import shell, shellcon
  7. import pythoncom
  8. import os, sys
  9. template_folder=os.path.split(sys.executable)[0]
  10. print('Template folder:', template_folder)
  11. template_pidl=shell.SHILCreateFromPath(template_folder,0)[0]
  12. template_pb=shell.SHGetViewStatePropertyBag(template_pidl, "Shell", shellcon.SHGVSPB_FOLDERNODEFAULTS, pythoncom.IID_IPropertyBag)
  13. # Column info has to be read as a stream
  14. # This may blow up if folder has never been opened in Explorer and has no ColInfo yet
  15. template_iunk=template_pb.Read('ColInfo',pythoncom.VT_UNKNOWN)
  16. template_stream=template_iunk.QueryInterface(pythoncom.IID_IStream)
  17. streamsize=template_stream.Stat()[2]
  18. template_colinfo=template_stream.Read(streamsize)
  19. def update_colinfo(not_used, dir_name, fnames):
  20. for fname in fnames:
  21. full_fname=os.path.join(dir_name,fname)
  22. if os.path.isdir(full_fname):
  23. print(full_fname)
  24. pidl=shell.SHILCreateFromPath(full_fname,0)[0]
  25. pb=shell.SHGetViewStatePropertyBag(pidl, "Shell", shellcon.SHGVSPB_FOLDERNODEFAULTS, pythoncom.IID_IPropertyBag)
  26. ## not all folders already have column info, and we're replacing it anyway
  27. pb.Write('ColInfo', template_stream)
  28. iunk=pb.Read('ColInfo',pythoncom.VT_UNKNOWN)
  29. s=iunk.QueryInterface(pythoncom.IID_IStream)
  30. s.Write(template_colinfo)
  31. s=None
  32. ## attribute names read from registry, can't find any way to enumerate IPropertyBag
  33. for attr in ('Address','Buttons','Col','Vid','WFlags','FFlags','Sort','SortDir','ShowCmd','FolderType','Mode','Rev'):
  34. pb.Write(attr, template_pb.Read(attr))
  35. pb=None
  36. os.path.walk(template_folder,update_colinfo,None)