Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

change folder icon with python

import os
import ctypes
from ctypes import POINTER, Structure, c_wchar, c_int, sizeof, byref
from ctypes.wintypes import BYTE, WORD, DWORD, LPWSTR, LPSTR
import win32api    

HICON = c_int
LPTSTR = LPWSTR
TCHAR = c_wchar
MAX_PATH = 260
FCSM_ICONFILE = 0x00000010
FCS_FORCEWRITE = 0x00000002
SHGFI_ICONLOCATION = 0x000001000    

class GUID(Structure):
    _fields_ = [
        ('Data1', DWORD),
        ('Data2', WORD),
        ('Data3', WORD),
        ('Data4', BYTE * 8)]

class SHFOLDERCUSTOMSETTINGS(Structure):
    _fields_ = [
        ('dwSize', DWORD),
        ('dwMask', DWORD),
        ('pvid', POINTER(GUID)),
        ('pszWebViewTemplate', LPTSTR),
        ('cchWebViewTemplate', DWORD),
        ('pszWebViewTemplateVersion', LPTSTR),
        ('pszInfoTip', LPTSTR),
        ('cchInfoTip', DWORD),
        ('pclsid', POINTER(GUID)),
        ('dwFlags', DWORD),
        ('pszIconFile', LPTSTR),
        ('cchIconFile', DWORD),
        ('iIconIndex', c_int),
        ('pszLogo', LPTSTR),
        ('cchLogo', DWORD)]

class SHFILEINFO(Structure):
    _fields_ = [
        ('hIcon', HICON),
        ('iIcon', c_int),
        ('dwAttributes', DWORD),
        ('szDisplayName', TCHAR * MAX_PATH),
        ('szTypeName', TCHAR * 80)]    

def seticon(folderpath, iconpath, iconindex):
    """Set folder icon.

    >>> seticon(".", "C:Windowssystem32SHELL32.dll", 10)

    """
    shell32 = ctypes.windll.shell32

    folderpath = unicode(os.path.abspath(folderpath), 'mbcs')
    iconpath = unicode(os.path.abspath(iconpath), 'mbcs')

    fcs = SHFOLDERCUSTOMSETTINGS()
    fcs.dwSize = sizeof(fcs)
    fcs.dwMask = FCSM_ICONFILE
    fcs.pszIconFile = iconpath
    fcs.cchIconFile = 0
    fcs.iIconIndex = iconindex

    hr = shell32.SHGetSetFolderCustomSettings(byref(fcs), folderpath,
                                              FCS_FORCEWRITE)
    if hr:
        raise WindowsError(win32api.FormatMessage(hr))

    sfi = SHFILEINFO()
    hr = shell32.SHGetFileInfoW(folderpath, 0, byref(sfi), sizeof(sfi),
                                SHGFI_ICONLOCATION)
    if hr == 0:
        raise WindowsError(win32api.FormatMessage(hr))

    index = shell32.Shell_GetCachedImageIndexW(sfi.szDisplayName, sfi.iIcon, 0)
    if index == -1:
        raise WindowsError()

    shell32.SHUpdateImageW(sfi.szDisplayName, sfi.iIcon, 0, index)
Comment

PREVIOUS NEXT
Code Example
Python :: python re.findall() 
Python :: PySimple list of elements 
Python :: numpy flatten along two axes 
Python :: element tree directory python 
Python :: reactstrap example 
Python :: how to round a number up in python 
Python :: django group permissions method 
Python :: Reversing Ints 
Python :: tkinter auto resize height 
Python :: geopandas with postgis 
Python :: como poner estado a un bot en discord 
Python :: python cassandra 
Python :: make virtual environment python 
Python :: How determine if a number is even or odd using Modulo Operator 
Python :: time in python 
Python :: how to mention a role discord.py 
Python :: openpyxl 
Python :: django 3 create async rest api 
Python :: python for loop practice problems 
Python :: pythagoras theorem formula 
Python :: switch case python 3.10 
Python :: pearsons correlation calculation 
Python :: python pandas dataframe conditional subset 
Python :: python write a line to a file 
Python :: false in py 
Python :: Python Tkinter MenuButton Widget 
Python :: create a file in a specific directory 
Python :: numpy cumsum 
Python :: copy array along axis numpy 
Python :: pygame rect 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =