The NameSpace method takes one argument. If you want to access a directory, you use a fully qualified path as the argument. For example, if you want to access the directory on the C drive, you specify
Set myFolder = _
sa.NameSpace("C:\")
If you want to access a special folder, you use a special-folder constant. Table 3 shows the constants for several main special folders. You can find a more complete list at http://msdn
.microsoft.com/library/default.asp?url=
/library/psdk/shellcc/shell/objects/
shell/shellspecialfolderconstants.htm. Or, you can go into Visual Studio's (VS's) shlobj.h file and locate all the constants that start with CSIDL. A third approach is to create a Visual Basic (VB) project, reference the Microsoft Shell library (Shell Controls and Automation is the description), use the Object Browser to go into the Shell32 library, and open the enumeration called ShellSpecialFolderConstants. Table 3 is a subset of this enumeration.
For example, suppose you want to access the special folder My Computer. You can use the code
Set myFolder = _
sa.NameSpace(ssfDRIVES)
After you reference the special-folder constant, you need to import that constant into your script. Although you can use Const statements, a better approach is to use the XML-based WSH file (.wsf) format. (To use the .wsf format, you need to run WSH 2.0 or later.)
Suppose you have a VBScript file, drives.vbs, that uses special-folder constants. Here's how you use the .wsf format to import the constants. First, create a file, myfile.wsf, and enter the code in Listing 3. This code simply wraps the .vbs file into a .wsf workspace. The advantage of using a .wsf workspace is that you can reference external COM objects and automatically import any declaration in the context in which the .vbs file will run. Now, you can use just one line of code to import, access, and display the My Computer folder:
MsgBox ssfDRIVES
With this approach, you avoid having to fill your .vbs file with numerous Const statements. Even better, this approach lets you develop the .vbs logic while avoiding any further contact with XML or other details of the .wsf syntax.
Next Month
Because Shell.Application is the Shell object model's root object, knowing how to use it is vital. I've covered the basics here; if you want more information about this object, go to http://msdn.microsoft.com/library/default.asp? url=/library/psdk/shellcc/shell/objects/shell/shell.htm.
With the Shell.Application object, you can browse folders, access applets, and access special folders. In addition, the Shell.Application object lets you access the Folder and FolderItem objects. I'll cover these objects in detail next month.