Subscribe to Windows IT Pro
September 27, 2000 12:00 AM

Understanding VBScript: The Windows Shell Object Model

Windows IT Pro
InstantDoc ID #15646
Rating: (2)
Downloads
15646.zip

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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Sep 07, 2005

    your explanation of difference between folders and directories could do with a cleanup. old directories could store other directories. in unix they can store 'symbolic links', device links, etc, as well.

  • Anonymous User
    8 years ago
    Oct 21, 2004

    All works perfect on my W2000 laptop, however fails miserably on my XP professional SP1 desktop.

    On the XP Prof the BrowseforFolder will not return single files. When selecting single files it returns error code 424 (= Cancel).

  • raphael AME
    9 years ago
    May 27, 2003

    All works perfectly when I create a Windows Shell Object locally. However, I get an error when I try to use it on a shared directory or on a Web server. Cannot understand could you help me?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.