Microsoft

Print a list of directory contents in Windows 2000 with DOS

Being able to print a listing of a directory's contents is handy, especially when preparing technical documentation. But Windows lacks this functionality. Leave it to good ol' DOS and the DIR command to save the day.


As a technical writer and consultant, I am constantly working with system documentation. One frequently requested element is a catalog of documents or other electronic files stored in various network directories.

Windows 95 and later allows for long file names, allowing documents and other files to bear a descriptive label. This system is much more intuitive than the 8.3 standard—eight-character file name and three-character extension—used by DOS. But unlike DOS, Windows 95 and later versions offer no easy way to capture a list of file names stored in various directories.

However, several versions of Windows still provide access to MS-DOS commands. Using these commands, you can quickly and easily create lists of file names.

Version note
This technique is dependent on accessing MS-DOS. Although I've tested it only under Windows 2000, the basic technique should work under all DOS versions.

Windows Explorer
Windows Explorer combined the best features of Windows 3.1’s Program Manager and File Manager into a single, powerful file-management utility. It lets you view the contents of any storage medium on your computer as icons or in an easily sortable detailed mode. It reduces many of the complexities of DOS’s file management system to an easy point-and-click interface.

One thing it doesn’t let you do, though, is create lists of a directory’s contents. If you’re viewing the Explorer address bar, you can copy the displayed path name, and by clicking twice—not double-clicking—on a file name, you can edit or copy that file name (see Figure A), but copying individual file names in this manner for pasting into a document isn’t exactly efficient.

Figure A
You can copy individual file names to paste into a document, but with many files, the process quickly becomes tedious.


However, DOS provides some capabilities that Windows Explorer doesn’t have. By launching a DOS session, you can capture the DIR command’s output in a way that lets you quickly and easily generate file listings.

To launch a DOS session, simply go to Start | Run, and then type command.com in to the resulting dialog box, as shown in Figure B.When you press Enter, a new window containing the DOS command prompt will appear.

Figure B
This shows an easy way to launch a DOS session under Windows 2000.


The DOS DIR command
Under Windows 2000, DOS has received some upgrades. To verify the version, issue the VER command while in a Windows 2000 DOS session, and if you see the output Microsoft Windows 2000 (Version 5.00.2195), this indicates that it's a new version of DOS.

One of the first commands a DOS user was likely to learn was DIR, which generated a listing of files in the current directory (optional parameters could, of course, return listings of other directories). If you’re familiar with the DIR command from previous versions of DOS, you’ll notice that its output is now a little different, as shown in Figure C. It lists long file names, file sizes, and creation dates.

Figure C
The DIR command’s output isn’t very useful in its raw form.


Unfortunately, this volume of information is more than you need, and capturing it would result in a heavy editing task. But you can augment the DIR comment with a set of optional switches, as shown below. These switches aren't case sensitive. Click here to see a list of DIR command line switches.

DIR command line switches
/B���� Uses bare format (no heading information or summary)
/L���� Forces lowercase display of file names
/O���� List by files in sorted order:
������������� N by name (alphabetical)
������������� S by size (smallest first)
������������� E by extension (alphabetical)
������������� D by date/time (oldest first)
������������� G group directories first
������������� - using this prefix reverses the sort order
/P���� Pauses after each screenful of information
/S���� Lists files in the specified directory and all subdirectories
/W���� Uses wide list format
/X���� Displays 8.3 versions of long file names
/?���� Displays a full list of command line switches



For my example, I’ll use the /b switch, which simply results in a list of file names, as shown in Figure D. If need be, you can add the /s switch, which lists file names in the current directory and all subdirectories.

Figure D
This output is ideal for capturing file names.


The /s switch is useful even in a directory that has no subdirectories, as it includes the file path in the output (Figure E).

Figure E
It’s easy to add the path to your file name output.


Redirection
The DIR command gives you the information you want, but it isn’t yet in a form you can use in a Windows document—you can’t copy and paste the contents of the DOS window. Instead, you’ll use another native DOS technique: output redirection. Rather than allow the DIR command’s output to appear onscreen as it does by default, you’ll send it to a text file. To do so, you’ll use the > redirection operator, like this:
dir/b /s > dirlist.txt

Of course, you can use any file name you like and you can precede it with a file path. When you press [Enter], you’ll simply see the command line reappear. However, if you close the DOS session by typing exit and you return to Windows, you’ll find a new text file that contains the file names, as shown in Figure F, ready to be copied and pasted into any Windows document.

Figure F
You can’t use the DIR command’s DOS output, but you can redirect it to a text file that you can use in Windows.

Editor's Picks

Free Newsletters, In your Inbox