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