In Windows 95 and later, the file system is just one possible place to store and search for information. As a result, the pre-Win95 hierarchy of files and directories no longer adequately describes the possible objects that you can now access in Windows. New types of folders such as the Recycle Bin, My Documents, and Printers forced the adoption of a new programming interface. In this new programming interface, Folder objects represent Windows shell folders that contain files or references to other types of objects. (The Folder object in the Windows Shell object model isn't related to the Folder object in the File System Objectâ€”FSOâ€”object model. The objects simply share the same name.) Because files no longer solely define a folder's content, more general objects called FolderItem objects represent the items in a folder. A FolderItem can be a file or a chunk of data that represents, for example, a printer or deleted file. A collection called FolderItems represents all the FolderItem objects in a folder.
Because a folder is essentially a container that holds data, you can use a common set of methods and properties (i.e., the Folder object's methods and properties) to access many different types of objects. As a container of data, a folder is able to describe itself in terms of name, title, path, and content. The content can vary widely depending on a folder's particular role. For this reason, each folder implements a special functionality: the ability to enumerate child items (i.e., FolderItem objects). Each child item, in turn, has fixed properties and methods that you use to manipulate it. Here's a look at how you use the methods and properties of the Folder and FolderItem objects.
Getting Started: Creating an Instance of the Folder Object
You typically don't create instances of the Folder object directly. Instead, use the NameSpace method with a pathname or virtual folder ID to create instances. Listing 1 shows the NameSpace method in action. In this code, the myFolder variable is of type Folder. (For information about how the NameSpace method works, see "Understanding VBScript: The Windows Shell Object Model," October 2000.)
After you create an instance of the Folder object, you can use its properties and methods. The Folder Object exposes three properties:
- Applicationâ€”returns a reference to the folder's Application object
- ParentFolderâ€”returns a reference to the folder's parent Folder object
- Titleâ€”returns the folder's display name (i.e., the name that appears in Windows Explorer)
Table 1 shows the Folder object's methods. As you can see, the functions that you can perform with these methods are quite similar to the functions you can perform manually in the Windows shell. The method that you'll probably use the most is Items. Before I discuss the Items method, though, let me first show you how to use the Folder object's CopyHere, MoveHere, GetDetailsOf, and ParseName methods and discuss the limitations of the NewFolder method and Count property.
Copying and Moving Folders
The CopyHere method has the syntax
The mandatory first argument, elements, specifies the item or items to copy in the current folder. You can specify filenames, FolderItem objects, or FolderItems objects. The optional second argument, options, specifies how you want to perform the copy operation. Because CopyHere uses the same internal function as Windows Explorer to accomplish a copy operation, you might encounter Windows Explorer's animation if you copy several files or lengthy files. You can use flags to somewhat control how you want the copy operation to take place. Table 2 includes the most frequently used flags. For the complete list, check out the Microsoft Developer Network (MSDN) Online Library at http://msdn.microsoft.com/library/psdk/ shellcc/shell/structures/shfileopstruct.htm#shfileopstruct.
As Table 2 shows, each flag has a value. To use the flags, you assign the options argument the value of the flag or flags you want. For example, if you're running Windows 2000, you can efficiently duplicate files when you use CopyHere with the value of 8192 (i.e., &H2000) to copy an HTML page. When you use Microsoft Internet Explorer (IE) to save an HTML page, you obtain an .htm file and a subdirectory. The subdirectory contains all the accessory files (e.g., image files, scripts) for that page. If you use CopyHere with an option of 8192 under Win2K to copy an HTML page, CopyHere automatically copies the HTML page and all the accessory files in the subdirectory.
The MoveHere method's syntax is
MoveHere elements, \[options\]
MoveHere works the same way as CopyHere, except that MoveHere moves instead of copies the files into the current folder.
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 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.
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.