Subscribe to Windows IT Pro
December 11, 2001 12:00 AM

Scripting Solutions with WSH and COM: Short, Sharp Scripts

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

I decided to start the new year with a mixed bag of short, sharp, helpful scripts that you can incorporate into your scripts. All the scripts I discuss are easy to understand, and most have just two lines of code, making them good additions to your systems administration toolkit.

The Shell::Application Object
Microsoft Internet Explorer (IE) 4.0 introduced an object called Shell:: Application that can help you perform some unusual systems administration tasks. This object is native in Windows 2000, Windows Me, and Windows 98. If you use Windows NT 4.0 or Win95, you must be running IE 4.0 or later; however, if you use IE 5.0 with NT or Win95, the object might not load by default. If you experience that problem with IE 5.0 under NT 4.0, execute the command

IE5Setup.exe /C:"ie5wzd 
  /e:IE4Shell_NTx86 /I:Y"

To install the IE 5.0 object under Win95, run the command

IE5Setup.exe /C:"ie5wzd /e:IE4Shell_WIN /I:Y"

If you use either command, be sure to type it all on one line in the command shell window.

IE 5.0 isn't strictly a requirement, but on NT 4.0 or Win95 systems running IE 4.0, the Shell object doesn't install unless you explicitly turn on the Active Desktop option. You should be able to turn on Active Desktop, install the object, then turn off Active Desktop, but if that fails, I suggest you go to IE 5.0. For background information about the Shell object, including how to install it, see Dino Esposito, "Understanding VBScript: The Windows Shell Object Model," October 2000, Instant Doc ID 15646.

When you copy a group of files on a Windows machine by dragging them, a progress window displays an animation of a piece of paper flying from one folder to another. The Shell::Application object lets you use the copy function and the animated progress window in your scripts. The code in Listing 1 displays the animated progress box while the code creates an instance of the Shell::Application object, uses that object's NameSpace method to create a Folder object (i.e., C:\my scripts), then uses the Folder object's CopyHere method to copy files from the C:\alistair folder to the C:\my scripts folder. The method copies the entire subfolder structure from C:\alistair. However, you can easily specify just one file or use a wildcard to specify a range of files.

The progress window includes a Cancel button that you can click to cancel the copy task. However, the CopyHere method provides no rollback, so if you cancel the task, you must clean up the files.

Another use of the Shell::Application object is to move a group of files. AnimatedMove.vbs, which Web exclusive Listing 1 shows, uses the Folder object's MoveHere method to move the entire C:\alistair subtree, including the alistair folder, into the C:\my scripts folder. (You can find Web exclusive listings and code by going to http://www.winscriptingsolutions.com and entering the InstantDoc ID.) You can also use the Shell:Application object to perform numerous shell functions, as Web exclusive Listing 2 shows. The Shell::Application object's MinimizeAll method minimizes all windows on a user's screen, and the object's UndoMinimizeALL method returns the windows to their unminimized state. This script is useful when you want to be sure a user knows that a script is running and prevent active windows from hiding subsequent warning boxes. Remember to perform some function between the MinimizeAll and UndoMinimizeAll functions; otherwise, they just cancel each other.

SelectFolder.vbs lets the user select a folder from a treeview window. Listing 2 shows SelectFolder.vbs, which uses the Shell::Application object's BrowseForFolder method to create a treeview window in which the user can select a folder. The code then returns the Folder object that represents that folder. You can customize how the window appears and acts by passing as many as four parameters to the method. The first parameter provides a window handle; for your scripts, the first parameter should always be zero. The second parameter is a text string to display in the window's upper left corner—in Listing 2, I've specified Please select a file. Figure 1 shows where the specified text string appears. You can use the third parameter to display only certain objects (e.g., printers). The values for this parameter are quite complex, so I won't discuss them in detail. To display all objects, use the value -1. The fourth parameter (an optional parameter that my script doesn't use) lets you restrict the user to picking a folder from a specific subtree, such as C:\ or C:\my documents. To use this parameter, enclose the path in quotation marks; the user then will be unable to access anything above the specified part of the tree.

The BrowseForFolder method returns a standard Folder object, which you can manipulate just as you manipulate folders in Windows Script Host (WSH). For example, you can access the Folder::Title property, use the Folder::CopyHere or Folder:: MoveHere method as I explained earlier, or use the Folder::Items method to retrieve the folder's contents.

Shutdown.vbs, which Listing 3 shows, automatically displays the shutdown dialog box on a machine. Other Shell object methods let you pull up a host of other dialog boxes, such as the Run dialog box and the Windows Help dialog box. You can learn more about these and other Shell object methods on the Microsoft Developer Network (MSDN) at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/objects/shell/shell.asp.

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.