3

As this SU answer notes, in order to change a folder's icon, one has to change a folder's attribute to read-only or system, and have its desktop.ini contain something like

[.ShellClassInfo]
IconResource=somePath.dll,0

While it would be straightforward to use win32api.SetFileAttributes(dirpath, win32con.FILE_ATTRIBUTE_READONLY) and create the desktop.ini from scratch, I'd like to preserve other customizations present in a potentially existing desktop.ini. But should I use ConfigParser for that or does e.g. win32api (or maybe ctypes.win32) provide native means to do so?

Community
  • 1
  • 1
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
  • (the only customization-related function I found so far is [SHSetLocalizedName](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762250%28v=vs.85%29.aspx)) – Tobias Kienzler Nov 06 '14 at 09:45
  • Hello, as this thread is not that old, I allow myself to ask you if you have found an answer. I am currently facing the same issue. – DrHaze Feb 12 '15 at 16:10
  • @DrHaze unfortunately I stuck with a desktop.ini from scratch so far... maybe a small bounty on this question helps motivating someone into reporting their findings – Tobias Kienzler Feb 12 '15 at 17:33

1 Answers1

1

Ok, so from this thread, I managed to get something working. I hope that it will help you.

Here is my base desktop.ini file:

[.ShellClassInfo]
IconResource=somePath.dll,0

[Fruits]
Apple = Blue
Strawberry = Pink

[Vegies]
Potatoe = Green
Carrot = Orange

[RandomClassInfo]
foo = somePath.ddsll,0

Here is the script I use:

from ConfigParser import RawConfigParser

dict = {"Fruits":{"Apple":"Green", "Strawberry":"Red"},"Vegies":{"Carrot":"Orange"}  }
# Get a config object
config = RawConfigParser()
# Read the file 'desktop.ini'
config.read(r'C:\Path\To\desktop.ini')

for section in dict.keys():
    for option in dict[section]:
        try:
            # Read the value from section 'Fruit', option 'Apple'
            currentVal = config.get( section, option )
            print "Current value of " + section + " - " + option + ": " + currentVal
            # If the value is not the right one
            if currentVal != dict[section][option]:
                print "Replacing value of " + section + " - " + option + ": " + dict[section][option]
                # Then we set the value to 'Llama'
                config.set( section, option, dict[section][option])
        except:
            print "Could not find " + section + " - " + option 

# Rewrite the configuration to the .ini file
with open(r'C:\Path\To\desktop.ini', 'w') as myconfig:
    config.write(myconfig)

Here is the output desktop.ini file:

[.ShellClassInfo]
iconresource = somePath.dll,0

[Fruits]
apple = Green
strawberry = Red

[Vegies]
potatoe = Green
carrot = Orange

[RandomClassInfo]
foo = somePath.ddsll,0

The only problem I have is that the options are loosing their first letter uppercase.

Community
  • 1
  • 1
DrHaze
  • 1,318
  • 10
  • 22