Find all Windows Shell Folder (Special Folder) Locations

Quick method to find the location of the current user’s special folders and return them in a dictionary keyed on the folder name.

Two ways below to enum all the shell folder registry keys. The for loop version shown is safer. The while components are there but commented out, just note that it relies on an exception error to indicate the end of the key has been reached, so there is a small chance that some unknown condition could cause it to never terminate, so the for loop is safer.

user_dict = Get_User_ShellFolders()
print user_dict

def Get_User_ShellFolders():
    # Routine to grab all the Windows Shell Folder locations from the registry.  If successful, returns dictionary
    # of shell folder locations indexed on Windows keyword for each; otherwise, returns an empty dictionary.
    import _winreg
    return_dict = {}

    # First open the registry hive
        Hive = _winreg.ConnectRegistry(None, _winreg.HKEY_CURRENT_USER)
    except WindowsError:
        print "Can't connect to registry hive HKEY_CURRENT_USER."
        return return_dict

    # Then open the registry key where Windows stores the Shell Folder locations
        Key = _winreg.OpenKey(Hive, "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
    except WindowsError:
        print "Can't open registry key Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders."
        return return_dict

    # Nothing failed above, so enumerate through all the Shell Folder values and return in a dictionary
    # This relies on error at end of 
        #i = 0
        #while 1:
        for i in range(0, _winreg.QueryInfoKey(Key)[1]):
            name, value, val_type = _winreg.EnumValue(Key, i)
            return_dict[name] = value
            i += 1
        _winreg.CloseKey(Key)                           # Only use with for loop
        _winreg.CloseKey(Hive)                          # Only use with for loop
        return return_dict                              # Only use with for loop
    except WindowsError:
        # In case of failure before read completed, don't return partial results
        return {}

Example Output:

        'Templates': u'C:\\Documents and Settings\\username\\Templates', 
        'Cookies': u'C:\\Documents and Settings\\username\\Cookies', 
        'Fonts': u'C:\\WINNT\\Fonts', 
        'PrintHood': u'C:\\Documents and Settings\\username\\PrintHood', 
        'AppData': u'C:\\Documents and Settings\\username\\Application Data', 
        'Programs': u'C:\\Documents and Settings\\username\\Start Menu\\Programs', 
        'Personal': u'C:\\Documents and Settings\\username\\My Documents', 
        'Local Settings': u'C:\\Documents and Settings\\username\\Local Settings', 
        'Cache': u'C:\\Documents and Settings\\username\\Local Settings\\Temporary Internet Files', 
        'Startup': u'C:\\Documents and Settings\\username\\Start Menu\\Programs\\Startup', 
        'Desktop': u'C:\\Documents and Settings\\username\\Desktop', 
        'Start Menu': u'C:\\Documents and Settings\\username\\Start Menu', 
        'NetHood': u'C:\\Documents and Settings\\username\\NetHood', 
        'Local AppData': u'C:\\Documents and Settings\\username\\Local Settings\\Application Data', 
        'Favorites': u'C:\\Documents and Settings\\username\\Favorites', 
        'SendTo': u'C:\\Documents and Settings\\username\\SendTo', 
        'History': u'C:\\Documents and Settings\\username\\Local Settings\\History', 
        'My Pictures': u'C:\\Documents and Settings\\username\\My Documents\\My Pictures', 
        'Recent': u'C:\\Documents and Settings\\username\\Recent'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.