Skip to content

Instantly share code, notes, and snippets.

@TigersWay
Created December 2, 2016 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TigersWay/030029604355df18f8f07060c067a325 to your computer and use it in GitHub Desktop.
Save TigersWay/030029604355df18f8f07060c067a325 to your computer and use it in GitHub Desktop.
[Windows|Python] Try to re-customize folders (via desktop.in) after some Dropbox unsolved issue, or some backup recovery.
import sys, os, configparser
import win32api
FILE_ATTRIBUTE_READONLY = 0x01
FILE_ATTRIBUTE_HIDDEN = 0x02
FILE_ATTRIBUTE_SYSTEM = 0x04
FILE_ATTRIBUTE_NORMAL = 0x80
def customizeFolder(aFolder, depth = 2):
# Is that folder supposed to be customized?
if not os.path.isfile(os.path.join(aFolder, 'folder.ico')):
return
print(aFolder)
iniCfg = configparser.ConfigParser()
iniCfg.optionxform = str # Keep cases for section, keys & values
iniFilename = os.path.join(aFolder, 'desktop.ini')
icoFilename = os.path.join(aFolder, 'folder.ico')
try:
iniCfg.read_file(open(iniFilename))
win32api.SetFileAttributes(iniFilename, FILE_ATTRIBUTE_NORMAL)
except FileNotFoundError as e:
iniCfg.add_section('.ShellClassInfo')
finally:
iniCfg['.ShellClassInfo']['IconResource'] = '.\\folder.ico,0'
try:
iniCfg.remove_option('.ShellClassInfo', 'IconFile')
except NoSectionError as e:
pass
try:
iniCfg.remove_option('.ShellClassInfo', 'IconIndex')
except NoSectionError as e:
pass
# Set or reser all needed attributes
with open(iniFilename, 'w') as iniFile:
iniCfg.write(iniFile)
win32api.SetFileAttributes(iniFilename, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)
win32api.SetFileAttributes(icoFilename, FILE_ATTRIBUTE_HIDDEN)
win32api.SetFileAttributes(aFolder, FILE_ATTRIBUTE_READONLY)
# Should we try deeper?
if depth > 0:
for folder in os.listdir(aFolder):
folder = os.path.join(aFolder, folder)
if os.path.isdir(folder):
customizeFolder(folder, depth-1)
# For each path passed on the command line, try to re-customize!
for path in sys.argv[1:]:
if os.path.isdir(path):
customizeFolder(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment