Performing Actions on Files

Microsoft® Windows® 2000 Scripting Guide

When you right-click a Shell object in Windows Explorer, a shortcut menu appears showing actions that can be performed on that object. For example, when you right-click a VBScript file, you see such options as Open, Edit, Print, Create Shortcut, and Delete.

Within the Shell nomenclature, each of the commands that appear on a shortcut menu is known as a verb. A verb is a text string in the registry that specifies the command to be run when the menu item is clicked. Different objects have a different set of verbs. The Recycle Bin, for example, does not have a Print option, but it does have an Empty Recycle Bin option.

The FolderItem object features an InvokeVerbEx method that allows you to execute any of the verbs associated with an object. For example, this command opens the associated file:

objFile.InvokeVerbEx("Open")

Calling InvokeVerbEx without specifying a verb automatically invokes the default verb for the object. For example, with a Windows Media file, the default verb is Play; for a folder, the default verb is Open.

Be aware that verbs always function in exactly the same way they do when clicked in Windows Explorer. If you right-click a file and select Open, the file automatically opens. If you select Delete, however, a confirmation box is displayed before the file is deleted. This same confirmation box appears if you attempt to delete any object using InvokeVerbEx. For example, using the Delete verb on a file results in a dialog box similar to the one shown in Figure 11.8.

Figure 11.8 Confirmation Dialog Box

sas_fil_08s

By comparison, right-clicking a file and selecting Print does not display a dialog box; instead, the file is automatically printed to the default printer. If you use InvokeVerbEx to print a file programmatically, that file is automatically printed to the default printer without displaying a dialog box of any kind.

Scripting Steps

Listing 11.27 contains a script that prints all the files in the folder C:\Logs. To carry out this task, the script must perform the following steps:

  1. Create a variable named TargetFolder, and set the value to C:\Logs. This is the folder where the files to be printed are stored.

  2. Create an instance of the Shell object.

  3. Use the Namespace method to return a Folder object representing C:\Logs.

  4. Use the Items method to return a collection of all the FolderItems within C:\Logs.

  5. Create a For Each loop to iterate through all the collection of FolderItems.

  6. For each item in the collection, use the InvokeVerbEx method to perform an action using one of the shortcut menu options for that item. In this script, that action is Print.

Listing 11.27 Performing Actions on Files

  
1
2
3
4
5
6
7
TargetFolder = "C:\Logs" 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem in colItems
 objItem.InvokeVerbEx("Print")
Next