Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

September 27, 2000 12:00 AM

Understanding VBScript: The Windows Shell Object Model

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

One of the most important changes that Windows 95 introduced is the use of the folder to organize files, programs, and other items that the Windows OS needs. Previously, Windows used only directories to organize the OS.

A directory is simply an entry in the file-system internal tables—it can contain only files, and those files must be at the file-system level. A folder is a container that can house any type of item (including files, other folders, and custom-information items) from any level in the OS. Thus, folders are more versatile than directories. Folders are powered by a piece of code (a sort of data provider) that enumerates all the items that the folder contains. Architecturally speaking, directories are folders.

Within Windows Explorer, folders look like directories, even if the folders don't contain any files. For example, the Printers folder looks like a regular folder, yet it contains printer names rather than filenames. Each item in the folder represents a piece of hardware rather than a file.

To list and process a directory's contents, you can use MS-DOS commands or the File System Object (FSO) model's FileSystemObject object. However, you can't use these tools to access the contents of special file-system folders such as Printers and Recycle Bin. Instead, you need to use the Shell object model. You can use Shell objects to access not only special folders but also file-system directories. In addition, you can use Shell to programmatically drive the Windows shell and programmatically access all of Windows Explorer's features.

Shell objects are well suited for use in the Windows Script Host (WSH) environment. However, they aren't natively available on all versions of Windows. The sidebar "Getting Shell Objects onto Your System" discusses which versions don't have these objects and how you can obtain them. After you have these objects in place, you access all the main features of the OS's GUI. Let's look at how you can use the Shell object model's root object to browse through folders, open Control Panel applets, and access special folders.

The Shell.Application Object
The Shell object model's root object is Shell.Application. To create an instance of this object in VBScript code, you use VBScript's CreateObject function:

Set sa = CreateObject _
("Shell.Application")

After you instantiate the object, you can use any of the methods that Table 1 shows. These methods make many system functions available to scripts. For example, to obtain the system dialog box that lets you search for files and folders, you use the FindFiles method:

sa.FindFiles

The resulting dialog box is the same one that appears when you select Find, then Files or Folders in the Start menu. The title of this dialog box differs depending on the OS you're running. In Windows NT, the title is Find: All Files.

The Shell.Application object's Explore and Open methods open a Windows Explorer window for a specified folder. The difference between these methods is that Explore creates a two-pane window that has the display tree in the left pane and Open creates a one-pane window that doesn't have the display tree.

To programmatically open the Taskbar Properties dialog box (i.e., the dialog box that appears when you click Start, Settings, Taskbar), you use the TrayProperties method:

sa.TrayProperties

Other useful system dialog boxes that you can open with the Shell.Application object are Run and Date/Time Properties. You use the FileRun and SetTime methods, respectively, to obtain them:

sa.FileRun
sa.SetTime

The system creates each dialog box asynchronously with respect to the rest of the VBScript code. In other words, the lines of code after these methods (and any other methods that create dialog boxes) execute as soon as the order to create the dialog box has been passed to the Shell and without waiting for the dialog box to appear. Depending on runtime conditions, the code

sa.FileRun
MsgBox "Hello, world"

can cause the message box to appear before the Run dialog box.

To programmatically shut down Windows, you can use the ShutdownWindows method. Running the code

Set sa = CreateObject _
("Shell.Application") _
sa.ShutdownWindows

has the same result as clicking Shut Down in the Start menu.

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

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

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