Share via


Enumerating Special Folders

Microsoft® Windows® 2000 Scripting Guide

The Windows operating system includes a number of special folders, folders that have a well-defined purpose and are generally present on all computers. These special folders include virtual folders, such as My Documents and Recycle Bin, as well as physical file system folders such as Program Files and Fonts. Because of the importance of these folders, it is useful for a system administrator to be able to locate and, if necessary, manipulate these folders on any computer.

However, there are at least two problems with identifying special folders on a computer. First, the location of these folders can vary. Mary might have her My Documents folder on drive C, Ken might have his My Documents folder on drive D, while Mike might have his My Documents folder located on a network drive. In addition, there is no guarantee that these three folders are all named My Documents. Special folders can be renamed; Mary might retain the name My Documents for this folder, but Ken might have renamed his My Documents folder to Ken Myer's Personal Folder.

Fortunately, the operating system does not use physical locations or names to keep track of special folders. Instead, special folders are tracked using CSIDLs, a standard method for identifying these objects regardless of their name or location. For example, the CSIDL for the Recycle Bin is this:

{645FF040-5081-101B-9F08-00AA002F954E}

Other special folders have similar CSIDLs.

Windows Script Host (WSH) can be used to retrieve the location of many of these special folders, particularly those that have a physical location. WSH can retrieve these locations by specifying a folder mnemonic (StartMenu, MyDocuments, Fonts). However, WSH cannot retrieve the location of all the special folders, particularly virtual folders (such as My Network Places) that do not have a physical location. In addition, WSH can only return the location of special folders; it cannot be used to enumerate the items within those folders or to perform any action on those folders.

If you need to locate any special folder on the computer or if you need to perform an action on a particular special folder, you can use the Shell object instead. The Shell object includes a number of predefined constants that can be passed to the Namespace method and return a folder object for a special folder. For example, the constant &H21& represents the Cookies folder; this code returns a folder object for the Cookies folder:

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H21&)

A list of Shell special folder constants and their Windows Script Host equivalents (where applicable) is shown in Table 11.5. WSH has one advantage over the Shell object: the WSH mnemonics (Programs for the Programs folder, MyDocuments for the My Documents folder) are easier to use and easier to remember than the Shell object constants. However, the Shell object can enumerate many more special folders than WSH can.

Table 11.5 Special Folder Constants and Their WSH Equivalents

Constant

Special Folder

WSH Equivalent

&H1&

Internet Explorer

None

&H2&

Programs

Programs

&H3&

Control Panel

None

&H4&

Printers and Faxes

None

&H5&

My Documents

MyDocuments

&H6&

Favorites

Favorites

&H7&

Startup

Startup

&H8&

My Recent Documents

Recent

&H9&

SendTo

SendTo

&Ha&

Recycle Bin

None

&Hb&

Start Menu

StartMenu

&Hd&

My Music

None

&He&

My Videos

None

&H10&

Desktop

Desktop

&H11&

My Computer

None

&H12&

My Network Places

None

&H13&

NetHood

Nethood

&H14&

Fonts

Fonts

&H15&

Templates

Templates

&H16&

All Users Start Menu

AllUsersStartMenu

&H17&

All Users Programs

AllUsersPrograms

&H18&

All Users Startup

AllUsersStartup

&H19&

All Users Desktop

AllUsersDesktop

&H1a&

Application Data

None

&H1b&

PrintHood

PrintHood

&H1c&

Local Settings\Application Data

None

&H19&

All Users Favorites

None

&H20&

Local Settings\ Temporary Internet Files

None

&H21&

Cookies

None

&H22&

Local Settings\History

None

&H23&

All Users Application Data

None

&H24&

Windows

None

&H25&

System32

None

&H26&

Program Files

None

&H27&

My Pictures

None

&H28&

User Profile

None

&H2b&

Common Files

None

&H2e&

All Users Templates

None

&H2f&

Administrative Tools

None

&H31&

Network Connections

None

Not all computers have each of the special folders shown in Table 11.5. However, even if a folder does not exist, the name and location of that folder are predefined by the operating system. As a result, a script that attempts to enumerate special folders does not fail even if some of these folders do not exist.

Scripting Steps

Listing 11.6 contains a script that returns the path to the My Pictures folder. To carry out this task, the script must perform the following steps:

  1. Create a constant named MY_PICTURES and set the value to &H27&.

  2. Create an instance of the Shell object.

  3. Use the Namespace method to return a Folder object representing the My Pictures folder. This is done by passing the constant MY_PICTURES as the parameter for the Namespace method.

  4. Use the Self method to return a FolderItems object for the My Pictures folder. This is required because only FolderItems objects possess the Path property, which allows you to resolve the actual path to the My Pictures folder. The Folder object returned in step 3 can return the name of the My Pictures folder but not the path.

  5. Echo the name and path to the My Pictures folder.

Listing 11.6 Enumerating Special Folders

  
1
2
3
4
5
Const MY_PICTURES = &H27&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_PICTURES)
Set objFolderItem = objFolder.Self 
Wscript.Echo objFolderItem.Name & ": " & objFolderItem.Path