The old DOS-based DIR command has been with us for quite a while. It’s handy for screen prints of files and subfolders, but using it to send that information to a printer is cumbersome. A GUI interface would make more sense for this task, so I’ve created a nifty little script you can download that uses Windows Script Host and VBScript to take advantage of Windows Script Host’s Office abilities to automate Excel.
In this case, the script uses several file system properties to gather the names, dates, and sizes of all the files and subfolders in a particular folder. The script then uses Office automation to compile all this information in a neatly formatted Excel spreadsheet. Once the directory listing is in the spreadsheet, the script will automatically print the listing and close Excel. And, best of all, I’ve discovered a way to integrate the script into the Windows operating system so you can simply right-click on a folder in Windows Explorer and select the Print Directory command. In this Daily Drill Down, I’ll introduce you to my Print Directory script and show you how to add it to the context menu. As I do, I’ll explain how the script works.
In order to use the Print Directory script, Excel 2000 or 2002 must be installed on your computer. The script may run with Excel 97, but I was unable to test it with this version of Excel. If you’re running Excel 97, please post a message in the Discussion Center and let me know if the script works with the earlier version. The Print Directory script is also designed to work in Windows 98/Me, Windows 2000, and Windows XP. It may work in Windows NT and Windows 95, but not reliably.
Installing the Print Directory script
Once you’ve downloaded the Print Directory Script.zip archive file, installing the script on your hard disk is easy. To do so, create a folder in the root directory of drive C: called PrintDir. Keep in mind that the script is specifically designed to run from a folder named PrintDir. If you name the folder something other than PrintDir, the script won’t work correctly.
Then, unzip the Print Directory Script.zip archive file and copy these two files to the PrintDir folder:
- · PrintDir.bat: Standard DOS-based batch file that simply launches the script.
- · PrintDir.vbs: The Windows Script Host and VBScript script file that does all the work.
Integrating the script into the operating system
Integrating the script into the operating system is a pretty straightforward procedure. However, there are a number of steps to take, so it’s imperative that you follow along closely. As I work through the procedure, I’ll use Windows XP as the example system for the screen shots. However, the procedure is almost identical in Windows 2000 and Windows 98/Me. If there are any differences, I’ll point them out as I go along; otherwise you can assume the steps are the same for all operating systems.
To begin, open Windows Explorer, pull down the Tools menu, and select Folder Options (in Windows 98, you’ll find Folder Options on the View menu). When you see the Folder Options dialog box, select the File Types tab. Now, scroll through the Registered File Types list and select the Folder option, as shown in Figure A, and then click the Advanced button (in Windows 98, you’ll click the Edit button).
|Locate and select the Folder type in the Registered File Types list box.|
When you do, the Edit File Type dialog box opens, as shown in Figure B (the Windows 98 Edit File Type dialog box is slightly larger, but the controls you’ll use are identical).
|The Edit File Type dialog box is designed to allow you to assign commands to the Folder type.|
To continue, click the New button—you’ll see the New Action dialog box, as shown in Figure C.
|You’ll use the New Action dialog box to actually integrate the script with the operating system.|
At this point, type Print Directory in the Action text box. This will become the command on the context menu. Next, click the Browse button adjacent to the Application Used To Perform Action text box and use the Open With dialog box to locate and select the PrintDir.bat file, which will be in the C:\PrintDir folder, as shown in Figure D. To continue, click OK (if you’re using Windows 98 or Windows Me, you may want to read the sidebar “Configuring the batch file in Windows 98/Me” before you perform this step).
|You’ll use the Open With dialog box to locate and select the batch file.|
Why use a batch file?
The batch file is necessary for this technique to function. You’ll notice that the Files Of Type list box specifies Programs as the default file type. This is because Windows will only accept what it considers to be executable files for the Application Used To Perform Action Setting. In this case, that means files with an .exe, .com, .pif, or a .bat extension—it won’t accept files with a .vbs extension. Therefore, I need to use a batch file to link the script to the operating system.
At this point, your New Action dialog box will look like the one shown in Figure E. You can click OK to close the dialog box.
|Your New Action dialog box should now look like this one.|
When you return to the Edit File Type dialog box, you’ll see that the Print Directory command now appears in the Actions list box, as shown in Figure F.
|The new Print Directory command now appears in the Actions list box.|
To complete the procedure, click OK to close the Edit File Type dialog box, and then click Close to close the Folder Options dialog box (in Windows 98, you’ll click the Close button twice—once to close the Edit File Type dialog box and once to close the Folder Options dialog box).
Using the new command
Now that you’ve created the Print Directory command, printing a list of the files in a folder is easy. When you’re using Windows Explorer, simply right-click on a folder in the Folders Explorer Bar and select the Print Directory command from the context menu, as shown in Figure G.
|You can now print a directory simply by right-clicking on a folder in the Folders Explorer Bar and selecting the Print Directory command from the context menu.|
Once you select the command, you’ll momentarily see a command prompt window appear on the screen (in Windows 98/Me the window is titled MS-DOS Prompt). The batch file causes this screen to appear as it launches the script. Next you’ll see Microsoft Excel appear on the screen—you can watch as the script creates the spreadsheet containing the directory listing. As soon as the creation phase is complete, the script will instruct Excel to print the file and then immediately close the application. The script will then terminate. In a few minutes, you’ll find a nicely formatted directory listing report coming out of your printer.
Removing the command from the Folder context menu
If you decide that you want to remove the Print Directory command from the Folder context menu, you’ll discover that you can’t do so from within the Edit File Type dialog box in Windows Me, Windows 2000, or Windows XP—even though there’s a Remove button. When you select the Print Directory Action in the list, the Remove button remains inactive (in Windows 98, you can select the Print Directory Action in the list and the Remove button will be active).
To remove the Print Directory command from the Folder context menu in Windows Me, Windows 2000, or Windows XP, you’ll need to delve into the registry and manually delete the command. Fortunately, it’s an easy procedure—just launch the Registry Editor by accessing the Run dialog box and typing Regedit.exe in the Open text box. Once the Registry Editor is up and running, navigate to the HKEY_CLASSES_ROOT\Folder\shell key. You’ll see a subkey titled Print_Directory. Simply right-click on the Print_Directory subkey and select the Delete command. When you do, you’ll be prompted to confirm the delete operation. If you’re sure that you selected the correct subkey to delete, just click Yes. Then close the Registry Editor.
Careful with that registry!
Always use caution when working with the registry. Backing up the system before using the registry is always a good idea.
Once the script creates the Excel spreadsheet containing the directory listing, it prints the list and then immediately closes the spreadsheet and Excel. If you’d like to investigate the directory listing in Excel and manually issue the Print command without the script closing the spreadsheet and Excel, you can do it by making two simple modifications to the script. Just load the script into Notepad, scroll about halfway through, and look for the lines:
When you find them, simply add a single quote to each line, as shown here:
‘ Call PrintSpreadSheet()
‘ Call CloseSpreadSheet()
This will turn those two commands into comments and basically remove them from the script. You can then save the script. Now when you run the Print Directory command, the spreadsheet will remain on the screen and you can study the listing in more detail before you print it.