Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active September 7, 2019 05:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maphew/47e67b6a99e240f01aced8b6b5678eeb to your computer and use it in GitHub Desktop.
Save maphew/47e67b6a99e240f01aced8b6b5678eeb to your computer and use it in GitHub Desktop.
Windows user "special folders" (Desktop, Start Menu, Documents, ...) info by enumerating constants from MS published list
'''List all User Shell Folders via ID number.
An alternative to the usual
objShell = win32com.client.Dispatch("WScript.Shell")
allUserProgramsMenu = objShell.SpecialFolders("AllUsersPrograms")
because "These special folders do not work in all language locales, a preferred
method is to query the value from User Shell folders"
Assumes python 3.6+ (for f-strings)
Sources:
https://stackoverflow.com/questions/2063508/find-system-folder-locations-in-python
https://ss64.com/vb/special.html
https://ss64.com/nt/shell-folders-vbs.txt
https://docs.microsoft.com/en-gb/windows/win32/api/shldisp/ne-shldisp-shellspecialfolderconstants#constants
'''
import win32com.client
import csv
shapp = win32com.client.Dispatch("Shell.Application")
csvfile = r"special-folder-constants.csv"
data = csv.DictReader(open(csvfile))
for row in data:
name = row["UserFolder"]
path = shapp.namespace(int(row["ID"])).self.path
print(f'{name:<20} {path}')
del data
'''Get user special folder by name'''
import win32com.client
import csv
shapp = win32com.client.Dispatch("Shell.Application")
csvfile = r"special-folder-constants.csv"
data = csv.DictReader(open(csvfile))
def get_by_name(name, data=data):
# print(name.upper())
for row in data:
if name.upper() == row["UserFolder"]:
# print(row["ID"])
return shapp.namespace(int(row["ID"])).self.path
return None
if __name__ == "__main__":
print("-"*40)
name = 'startmenu'
path = get_by_name(name)
print("Name:\t{}".format(name))
print("Path:\t{}".format(path))
UserFolder ID Description
DESKTOP 0 Windows desktop—the virtual folder that is the root of the namespace.
PROGRAMS 2 File system directory that contains the user's program groups (which are also file system directories). A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs.
CONTROLS 3 Virtual folder that contains icons for the Control Panel applications.
PRINTERS 4 Virtual folder that contains installed printers.
PERSONAL 5 File system directory that serves as a common repository for a user's documents. A typical path is C:\Users\username\Documents.
FAVORITES 6 File system directory that serves as a common repository for the user's favorite URLs. A typical path is C:\Documents and Settings\username\Favorites.
STARTUP 7 File system directory that corresponds to the user's Startup program group. The system starts these programs whenever any user first logs into their profile after a reboot. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\StartUp.
RECENT 8 File system directory that contains the user's most recently used documents. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Recent.
SENDTO 9 File system directory that contains Send To menu items. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\SendTo.
BITBUCKET 10 Virtual folder that contains the objects in the user's Recycle Bin.
STARTMENU 11 File system directory that contains Start menu items. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu.
DESKTOPDIRECTORY 16 File system directory used to physically store the file objects that are displayed on the desktop. It is not to be confused with the desktop folder itself, which is a virtual folder. A typical path is C:\Documents and Settings\username\Desktop.
DRIVES 17 My Computer—the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel. This folder can also contain mapped network drives.
NETWORK 18 Network Neighborhood—the virtual folder that represents the root of the network namespace hierarchy.
NETHOOD 19 A file system folder that contains any link objects in the My Network Places virtual folder. It is not the same as ssfNETWORK, which represents the network namespace root. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Network Shortcuts.
FONTS 20 Virtual folder that contains installed fonts. A typical path is C:\Windows\Fonts.
TEMPLATES 21 File system directory that serves as a common repository for document templates.
COMMONSTARTMENU 22 File system directory that contains the programs and folders that appear on the Start menu for all users. A typical path is C:\Documents and Settings\All Users\Start Menu. Valid only for Windows NT systems.
COMMONPROGRAMS 23 File system directory that contains the directories for the common program groups that appear on the Start menu for all users. A typical path is C:\Documents and Settings\All Users\Start Menu\Programs. Valid only for Windows NT systems.
COMMONSTARTUP 24 File system directory that contains the programs that appear in the Startup folder for all users. A typical path is C:\Documents and Settings\All Users\Microsoft\Windows\Start Menu\Programs\StartUp. Valid only for Windows NT systems.
COMMONDESKTOPDIR 25 File system directory that contains files and folders that appear on the desktop for all users. A typical path is C:\Documents and Settings\All Users\Desktop. Valid only for Windows NT systems.
APPDATA 26 Version 4.71. File system directory that serves as a common repository for application-specific data. A typical path is C:\Documents and Settings\username\Application Data.
PRINTHOOD 27 File system directory that contains any link objects in the Printers virtual folder. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Printer Shortcuts.
LOCALAPPDATA 28 Version 5.0. File system directory that serves as a data repository for local (non-roaming) applications. A typical path is C:\Users\username\AppData\Local.
ALTSTARTUP 29 File system directory that corresponds to the user's non-localized Startup program group.
COMMONALTSTARTUP 30 File system directory that corresponds to the non-localized Startup program group for all users. Valid only for Windows NT systems.
COMMONFAVORITES 31 File system directory that serves as a common repository for the favorite URLs shared by all users. Valid only for Windows NT systems.
INTERNETCACHE 32 File system directory that serves as a common repository for temporary Internet files. A typical path is C:\Users\username\AppData\Local\Microsoft\Windows\Temporary Internet Files.
COOKIES 33 File system directory that serves as a common repository for Internet cookies. A typical path is C:\Documents and Settings\username\Application Data\Microsoft\Windows\Cookies.
HISTORY 34 File system directory that serves as a common repository for Internet history items.
COMMONAPPDATA 35 Version 5.0. Application data for all users. A typical path is C:\Documents and Settings\All Users\Application Data.
WINDOWS 36 Version 5.0. Windows directory. This corresponds to the %windir% or %SystemRoot% environment variables. A typical path is C:\Windows.
SYSTEM 37 Version 5.0. The System folder. A typical path is C:\Windows\System32.
PROGRAMFILES 38 Version 5.0. Program Files folder. A typical path is C:\Program Files.
MYPICTURES 39 My Pictures folder. A typical path is C:\Users\username\Pictures.
PROFILE 40 Version 5.0. User's profile folder.
SYSTEMx86 41 Version 5.0. System folder. A typical path is C:\Windows\System32, or C:\Windows\Syswow32 on a 64-bit computer.
PROGRAMFILESx86 42 Version 6.0. Program Files folder. A typical path is C:\Program Files, or C:\Program Files (X86) on a 64-bit computer.
r'''Windows user "special folders" (Desktop, Start Menu, Documents, ...) info
fpath(x) full path for folder name `x`
description(x) description for folder name `x`
names() list all special folder names we know about
print_all() display all SF names and paths
Example output:
----------------------------------------
Search: startmenu
----------------------------------------
Name: STARTMENU
Path: C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Start Menu
Desc: File system directory that contains Start menu items. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu.
----------------------------------------
This module is an alternative to the usual
objShell = win32com.client.Dispatch("WScript.Shell")
allmenu = objShell.SpecialFolders("AllUsersPrograms")
because "These special folders do not work in all language locales, a preferred
method is to query the value from User Shell folders". So we do that using the
integer IDs from "ShellSpecialFolderConstants Enumeration", published 4/12/2018.
Additionally it turns out using Shell.Application returns more folders than
Wscript.Shell, 38 vs 16.
Sources:
https://stackoverflow.com/questions/2063508/find-system-folder-locations-in-python
https://ss64.com/vb/special.html
https://ss64.com/nt/shell-folders-vbs.txt
https://docs.microsoft.com/en-gb/windows/win32/api/shldisp/ne-shldisp-shellspecialfolderconstants#constants
'''
import win32com.client
import csv
shapp = win32com.client.Dispatch("Shell.Application")
csvfile = "special-folder-constants.csv"
data = tuple(csv.DictReader(open(csvfile)))
# we wrap in a tuple so the rows are available all the time, and immutable
# otherwise content of `data` can change in every call, e.g. early return
# after finding our vaue.
def names(data=data):
'''Return list of possible user special folder names'''
names = []
for row in data:
names.append(row["UserFolder"])
return names
def description(name):
'''Return description for `name` as string'''
for row in data:
if name.upper() == row["UserFolder"]:
return row["Description"]
def get_sfname(name):
'''Return Windows special folder for `name` as string or None'''
for row in data:
if name.upper() == row["UserFolder"]:
return row["UserFolder"]
def fpath(name):
'''Return full path for `name`. Virtual folders begin with `::`'''
for row in data:
if name.upper() == row["UserFolder"]:
return shapp.namespace(int(row["ID"])).self.path
def print_all(data=data):
'''Display all user special folder names and paths'''
print('{:<20} {}'.format('Name', 'Path'))
for row in data:
name = row["UserFolder"]
path = shapp.namespace(int(row["ID"])).self.path
print(f'{name:<20} {path}')
if __name__ == "__main__":
search = 'startmenu'
sfname = get_sfname(search)
path = fpath(search)
desc = description(search)
print("-"*40)
print("Search:\t{}".format(search))
print("-"*40)
print("Name:\t{}".format(sfname))
print("Path:\t{}".format(path))
print("Desc:\t{}".format(desc))
print("-"*40)
# print("-"*40)
# print(names())
# print("-"*40)
# print_all()
@maphew
Copy link
Author

maphew commented Aug 29, 2019

Output:

DESKTOP              C:\Users\mattw\Desktop
PROGRAMS             C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
CONTROLS             ::{26EE0668-A00A-44D7-9371-BEB064C98683}\0
PRINTERS             ::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{2227A280-3AEA-1069-A2DE-08002B30309D}
PERSONAL             C:\Users\mattw\Documents
FAVORITES            C:\Users\mattw\Favorites
STARTUP              C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
RECENT               C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Recent
SENDTO               C:\Users\mattw\AppData\Roaming\Microsoft\Windows\SendTo
BITBUCKET            ::{645FF040-5081-101B-9F08-00AA002F954E}
STARTMENU            C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Start Menu
DESKTOPDIRECTORY     C:\Users\mattw\Desktop
DRIVES               ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
NETWORK              ::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}
NETHOOD              C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Network Shortcuts
FONTS                C:\Windows\Fonts
TEMPLATES            C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Templates
COMMONSTARTMENU      C:\ProgramData\Microsoft\Windows\Start Menu
COMMONPROGRAMS       C:\ProgramData\Microsoft\Windows\Start Menu\Programs
COMMONSTARTUP        C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
COMMONDESKTOPDIR     C:\Users\Public\Desktop
APPDATA              C:\Users\mattw\AppData\Roaming
PRINTHOOD            C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
LOCALAPPDATA         C:\Users\mattw\AppData\Local
ALTSTARTUP           C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
COMMONALTSTARTUP     C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
COMMONFAVORITES      C:\Users\mattw\Favorites
INTERNETCACHE        C:\Users\mattw\AppData\Local\Microsoft\Windows\INetCache
COOKIES              C:\Users\mattw\AppData\Local\Microsoft\Windows\INetCookies
HISTORY              C:\Users\mattw\AppData\Local\Microsoft\Windows\History
COMMONAPPDATA        C:\ProgramData
WINDOWS              C:\Windows
SYSTEM               C:\Windows\System32
PROGRAMFILES         C:\Program Files
MYPICTURES           C:\Users\mattw\Pictures
PROFILE              C:\Users\mattw
SYSTEMx86            C:\Windows\SysWOW64
PROGRAMFILESx86      C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools

@maphew
Copy link
Author

maphew commented Sep 3, 2019

I decided this was worth making a module out of, repo at https://github.com/maphew/winfolders (name subject to change)

@maphew
Copy link
Author

maphew commented Sep 7, 2019

Note: contrary to MSDN page at time of this writing, PROGRAMFILESx86 is 42 not 48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment