October 4th, 2004

How Can I Print Text Files Using a Script?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a bunch of text files in a folder. Is there a way to print those files using a script? I know I can use Microsoft Word to print the files from a script, but I don’t have Microsoft Word installed on that computer.

— MA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MA. Don’t tell anyone from Microsoft we said this, but that’s OK; you don’t need Microsoft Word just to print a few text files. Instead, you can use the Shell object – which is built right into the operating system – to print text files. And what’s cool about this is the fact that you can print these files even though the Shell object doesn’t have a Print method. Strange but true!

You can do this is because the Shell object has a method called InvokeVerbEx that lets you perform the tasks that show up when you right-click a file in Windows Explorer. Right-click one of your text files, and look at the context menu that pops up. You should see things like Open, Print, Edit, Cut, Copy, Delete, etc. The Shell object lets you perform all those tasks programmatically. In fact, here’s a script that prints all the files in the folder C:\Logs:

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

All we’re doing here is connecting to the C:\Logs folder and then grabbing a collection of all the items found in that folder. We store the set of files in the variable colItems, using this line of code:

Set colItems = objFolder.Items

Note that we’ve made it easy on ourselves in this example by making sure that C:\Logs contains only log files, and that we want to print each of those files. If C:\Logs contains other files, files that we don’t want to print, you’ll have include code that distinguishes between the files you want to print and the files you don’t want to print.

After stashing the set of files in colItems, we use a For Each loop to loop through all the items in the collection. For each item in the collection (in other words, for each file in the folder) we use the InvokeVerbEx method, and we tell InvokeVerbEx that we want to print each file:

objItem.InvokeVerbEx(“Print”)

That’s it. Run this script and all the files will dutifully print, one right after the other.

Now what if we wanted to do something else, something like, say, edit each file? No problem; just replace the Print parameter with the appropriate parameter name from the context menu:

TargetFolder = “C:\Logs” 
Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(TargetFolder) 
Set colItems = objFolder.Items
For Each objItem in colItems
    objItem.InvokeVerbEx(“Edit”)
Next

One thing to keep in mind is that when you use InvokeVerbEx your script replicates the same actions you see in Windows Explorer. For example, what happens in Windows Explorer if you right-click a file and choose Delete? That’s right: instead of the file being automatically deleted, a dialog box appears asking if you are sure you want to send the file to the Recycle Bin. That’s important, because the same dialog box will appear if you pass Delete to InvokeVerbEx. This script won’t automatically delete the files in C:\Logs, but instead pops up a confirmation dialog box for each file in the folder:

TargetFolder = “C:\Logs” 
Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(TargetFolder) 
Set colItems = objFolder.Items
For Each objItem in colItems
    objItem.InvokeVerbEx(“Delete”)
Next

Keep in mind, too, that you might not be able to use all the options that appear in the Context menu. For more information on determining which options are available to you and which ones are not, check out this section of the Microsoft Windows 2000 Scripting Guide.

Author

0 comments

Discussion are closed.