Enumerating Special Folders

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

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

ConstantSpecial FolderWSH Equivalent
&H1&Internet ExplorerNone
&H2&ProgramsPrograms
&H3&Control PanelNone
&H4&Printers and FaxesNone
&H5&My DocumentsMyDocuments
&H6&FavoritesFavorites
&H7&Startup Startup
&H8&My Recent DocumentsRecent
&H9&SendToSendTo
&Ha&Recycle BinNone
&Hb&Start MenuStartMenu
&Hd&My MusicNone
&He&My VideosNone
&H10&DesktopDesktop
&H11&My ComputerNone
&H12&My Network PlacesNone
&H13&NetHoodNethood
&H14&FontsFonts
&H15&TemplatesTemplates
&H16&All Users Start MenuAllUsersStartMenu
&H17&All Users ProgramsAllUsersPrograms
&H18&All Users StartupAllUsersStartup
&H19&All Users DesktopAllUsersDesktop
&H1a&Application DataNone
&H1b&PrintHoodPrintHood
&H1c&Local Settings\Application DataNone
&H19&All Users FavoritesNone
&H20&Local Settings\ Temporary Internet FilesNone
&H21&CookiesNone
&H22&Local Settings\HistoryNone
&H23&All Users Application DataNone
&H24&WindowsNone
&H25&System32None
&H26&Program FilesNone
&H27&My PicturesNone
&H28&User ProfileNone
&H2b&Common FilesNone
&H2e&All Users TemplatesNone
&H2f&Administrative ToolsNone
&H31&Network ConnectionsNone

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  

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net