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 ObjectFSOobject 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:
- Applicationreturns a reference to the folder's Application object
- ParentFolderreturns a reference to the folder's parent Folder object
- Titlereturns 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
CopyHere elements, [options]
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.