Subscribe to Windows IT Pro
October 25, 2000 12:00 AM

Understanding VBScript: The Shell Object Model’s Folder and FolderItem Objects

Windows IT Pro
InstantDoc ID #15847
Rating: (0)
Downloads
15847.zip

Getting Details from Folders
With the GetDetailsOf method, you can access the same columnar information that is available through Windows Explorer. The data in each column depends on the type of folder. For example, file folders have the Name, Size, Type, Modified, and Attributes columns.

The syntax of GetDetailsOf is

GetDetailsOf vItem, iColumn

The first argument, vItem, specifies the folder from which you want to access the columnar information. This item must be a FolderItem object. The second argument, iColumn, specifies the column you want to access. All column IDs are 0-based except for infotips (i.e., text that provides information at the shell level). For example, a column ID of 1 specifies the Size column for file folders and the Original Location column for the Recycle Bin folder. Infotips have a column ID of ­1. GetDetailsOf always returns columnar information in the form of a string.

In Win2K, you can create custom columns in Windows Explorer. A custom column is a column of data that the user defines through a DLL. For example, you can create a column applicable only to .bmp files that shows those files' size in pixels. You can't use GetDetailsOf to retrieve the values of custom columns.

Parsing the Name
The ParseName method creates a FolderItem object from a display name. ParseName lets you obtain the FolderItem object for a specific item in a folder. As the code in Listing 2 shows, you pass ParseName the display name of the item you want to access. In this case, you pass the display name autoexec.bat to ParseName.

Knowing the Folder Object's Limitations
Before you use the Folder object, you need to know about the limitations of the NewFolder method and the Count property. The name NewFolder suggests that you can use this method to create a subfolder under the current folder. However, I've never been able to make NewFolder work. For example, if you run the code in Listing 3, you receive an error message instead of a new folder.

If you want to know how many items are in a folder, you can use the Count property. However, the value that the property returns doesn't include subfolders and files for which you've activated the Hidden attribute. This limitation occurs regardless of the visibility settings you've set for hidden files.

Using the Items Method
The Items method returns a pointer to the FolderItems collection. Collections are helpful because they provide properties and methods that let you access the FolderItem objects.

You can use the FolderItem object to obtain in-depth technical information about the contents of folders. Table 3 lists all the properties that a FolderItem object makes available. These properties let you access information regarding the nature of an item, such as its name, type, date of last modification, fully qualified pathname, and whether the item is browsable. For example, the code at callout A in Listing 4, page 7, uses the Name and Type properties to obtain each item's name and type, respectively.

Because a folder is a child of another folder (except for the Desktop folder), you can also use the FolderItem object to obtain in-depth technical information about folders. For example, if you want to obtain the FolderItem object for the C:\ folder, you can use the code

Set myFolder = _
	sa.NameSpace("C:\")
Set fi = myFolder.Items.Item

In this code, the Folder object's Items method retrieves the FolderItems collection. The Item method of the FolderItems collection, in turn, retrieves the FolderItem object representing the C:\ folder.

The code in Listing 4 uses the Items method to display the file-system and Windows folders in the My Computer folder. Figure 1 shows an example of the dialog box that results when you run this code.

As Listing 4 shows, you create an instance of the My Computer folder, which you specify with the MYCOMPUTER constant. (See "Understanding VBScript: The Windows Shell Object Model" if you're unfamiliar with special-folder constants.) Because you want to include the folder's name in the dialog box's heading, you use the Folder object's Title property to retrieve the folder's display name. Next, you use a For Each...Next statement to loop through each item in My Computer, retrieving that item's name and type with the FolderItem object's Name and Type properties, respectively. After you use the Folder object's Count property to specify the number of items in the folder, you display the results.

The FolderItem object has two methods (i.e., Verbs and InvokeVerb) and numerous properties (e.g., Name) that let you programmatically manipulate Windows Explorer's toolbar commands. The Verbs method retrieves the FolderItemVerbs collection, which contains one FolderItemVerb object for each command associated with the item that appears in Windows Explorer's Contents of pane. For example, the code in Listing 5 uses the Verbs method to retrieve the FolderItemVerb objects that represent the commands associated with the My Computer folder. As callout A in Listing 5 shows, you must first retrieve the FolderItem object that represents the My Computer folder before you use the Verbs method to retrieve the FolderItemVerbs collection that contains the commands. You then use a For Each...Next statement to enumerate each command in the collection. In the toolbar, the letter that executes each command is underlined; in the toolbar's programming, an ampersand (&) specifies the letter to underline. When the FolderItemVerb object's Name property retrieves the command's name, the name includes the ampersand. For display purposes, you can eliminate the ampersands. As callout B in Listing 5 shows, you can use the Replace function to search each command name (v.Name) for the ampersand ("&"), replacing it with an empty string (""). Figure 2 shows the result.

You can use the FolderItem object not only to enumerate but also to execute toolbar commands programmatically. You can programmatically execute the commands two ways. In the first approach, you retrieve the FolderItem object representing the targeted folder, use the Verbs method to retrieve the FolderItemVerbs collection, and enumerate through that collection with the For Each...Next statement until you find the command you want to execute. You then call the FolderItemVerb object's DoIt method to execute the command.

A more efficient approach, however, is to use the FolderItem object's InvokeVerb method. For example, the code in Listing 6 demonstrates how you can use the InvokeVerb method to programmatically open the Microsoft Management Console (MMC) Computer Management snap-in, a dialog box available through the Manage command in the toolbar for the My Computer folder. As callout A in Listing 6 shows, you pass the command's name to the InvokeVerb method. In other words, the InvokeVerb method requires that the name include the ampersand, if applicable.

Next Month
To use the Shell object model, you need to run Win2K, Win98, or any other Windows OS running IE 4.01 or later. For more information about the Shell object model, check out the MSDN Online Library at http://msdn.microsoft.com/ library/default.asp?url=/library/psdk/ shellcc/shell/objects/shell/shell.htm.

Next month, look for my column under its new name, "VBScripting Solutions." This new name reflects a change in focus. For the past year and a half, I've taught you the basics of the VBScript language. Starting next month, I'll show you how to apply what you've learned so that you can use VBScript code to automate your systems administration tasks.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
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.