EnglishFrenchGermanItalianPortugueseRussianSpanish
Home arrow Script Collection arrow Find all Windows Shell Folder (Special Folder) Locations
Find all Windows Shell Folder (Special Folder) Locations PDF Print E-mail

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
    try:
        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
    try:
        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."
        _winreg.CloseKey(Hive)
        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 
    try:
        #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
        _winreg.CloseKey(Key)
        _winreg.CloseKey(Hive)
        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'
}



Comments (1)
RSS comments
1. 01-06-2009 14:51
 
Thanks, worked well for me.
Guest
 
Anon

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
E-mail
Comment:

Code:* Code
I wish to be contacted by email regarding additional comments

Last Updated ( Saturday, 16 June 2007 )
 
< Prev   Next >

Post your scripts

If you have interesting scripts to share with the community, please post them to this site. Hint: For syntax highlighting and correct Python intendation place your code between html tags <pre> and </pre>.

Suggested

RSS category feeds

RSS site feeds

My prefered Python IDE

My prefered Python editor is Pyscripter from MMExperts. It is not only an editor. Pyscripter is a full Python IDE including (remote) debugging, a class browser, and all other nice helpers which a full featured IDE needs.

Do you have a script for me ?

Do you have an interesting Python script which does some really cool thing on Windows ? Please post them to this site. It`s very simple - simply copy&paste it to this form. No login is requiered.

Hint: For syntax highlighting and correct Python intendation place your code between html tags <pre> and </pre>.

My prefered web framework

My prefered web framework for developing web applications is Django. Django calls itself The web framework for perfectionists with deadlines. It is a really fast, scalable and (thanks Python) the sexiest web framework of the world.