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 tablesit 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.