oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Printing Directory Listings

by Mitch Tulloch, author of Windows Server Hacks

"How can I print thee? Let me count the ways...."

Liz Browning would probably have written something like that in her blog if she lived today. Imagine her writing poetry on her laptop and saving it in a folder named C:\Sonnets, when suddenly she gets an email from her publisher saying he needs the titles of all the poems she's written so far. So Liz opens Windows Explorer and selects the C:\Sonnets folder to display a list of sonnets in the right-hand pane (Figure 1). Then she wonders, How can I print a list of titles of these files?

Figure 1
Figure 1. How can Liz print a list of titles of her sonnets for her publisher?

Funny how often users need to do this, yet there's no simple way to accomplish the simple task through the Windows GUI. Of course, if you are familiar with the command line or can do some scripting, it's not difficult. So, out of curiosity I decided to "count" the ways this simple task could be done in Windows; here's what I came up with.

1. Using the Command Line

The simplest way is to use the good old dir command with the /b switch to suppress everything except the filenames. For example, to display the names of files in C:\Sonnets, you do the following:

C:\>dir c:\sonnets /b
which produces:

But only three in all God's universe.doc
Go from me. Yet I feel that I shall stand.doc
I lift my heavy heart up solemnly.doc
I thought once how Theocritus had sung.doc
The face of all the world is changed, I think.doc
Thou hast thy calling to some palace-floor.doc
Unlike are we, unlike, O princely Heart!.doc

You can either redirect the output to a text file with:

dir c:\sonnets /b > titles.txt

Or you can redirect it to your default printer with:

dir c:\sonnets /b > prn

If you don't want to open a command-prompt window first, you can simply go to Start -> Run and type:

cmd /c dir c:\sonnets /b > lpt1

If the directory you want to list contains subdirectories, you can use tree instead of dir:

cmd /c tree c:\sonnets /f > lpt1

2. Adding a Context Menu Item

It would be nice to right-click on a folder in Windows Explorer and print a list of items in the folder, wouldn't it? This is easy to do by first creating the following batch script using Notepad:

@echo off
dir %1 /-p /o:gn > "%temp%\Listing"
start /w notepad /p "%temp%\Listing"
del "%temp%\Listing"

Save this with some name like printdir.bat in your %windir% directory, and add "Print Directory Listing" to your context menu for Windows Explorer as follows. Open the Folder Options tool in Control Panel, select the File Types tab, and in the File Types column select the item labeled File Folder. Then click on the Advanced button, click on New, and specify a name for the action you want to perform and the batch file that performs it (Figure 2):

Figure 2
Figure 2. Associating the batch file printdir.bat with the action "Print Directory Listing"

Once you're done, you can right-click on a folder in Windows Explorer and print a list of the files it contains (Figure 3):

Figure 3
Figure 3. Printing a directory listing from Windows Explorer

This hack is cool, but it has a flaw when you implement it on XP: after you've implemented it, every time you try to open a folder using Windows Explorer, the Search Companion window opens instead of the folder you selected. The workaround is a quick Registry edit described in Knowledge Base article 321379, where this hack is found. Of course, you can further customize the hack by tweaking the syntax of the dir command or replacing it with tree, and so on.

3. Using Windows Script Host

Windows Script Host lets you leverage the power of scripting languages like VBScript and JScript to automate almost any task you need to perform. For example, here's a simple JScript that will list the names of all the files in the C:\Sonnets folder:

var fso, e, file;
fso = new ActiveXObject("Scripting.FileSystemObject");
e = new Enumerator(fso.GetFolder("c:\sonnets").files);
for (e.moveFirst(); ! e.atEnd(); e.moveNext()) {
  file = e.item();

Saving this script as listdir.js and running it with Cscript.exe, the command-line script interpreter for WSH, produces the expected output (Figure 4):

Figure 4
Figure 4. Using WSH to print a directory listing

I found this useful script snippet in the book Windows XP Under the Hood: Hardcore Windows Scripting and Command Line Power by Brian Knittel. It's a terrific resource if you're still new to WSH scripting and want to explore its flexibility and power.

4. Quick and Dirty

Finally, if Liz doesn't feel confident working from the command line or developing scripts, she can send her publisher a directory listing of her C:\Sonnets folder using the following quick-and-dirty hack: press Alt-Print Screen, paste the contents of the clipboard into Paint, save it as a bitmap file, and send it as an email attachment. Obviously pretty crude, and you wouldn't think this approach would be worth its very own article in the Microsoft Knowledge Base, but you'd be wrong--heh!

Windows Server Hacks

Related Reading

Windows Server Hacks
100 Industrial-Strength Tips & Tools
By Mitch Tulloch

Mitch Tulloch is the author of Windows 2000 Administration in a Nutshell, Windows Server 2003 in a Nutshell, and Windows Server Hacks.

Return to the Windows DevCenter.