|
Script to Print a Directory File List
By Stephen Bucaro
How many times have you wished you could print a listing of all the files in a folder?
Note: People wonder why Microsoft doesn't add a file list printing menu item to Windows
Explorer. Microsoft wants you to interface with the computer through applications. They
don't want you to know the file system exists. That's why Windows Explorer has been relegated
to the Accessories menu. When I say "applications" what I really mean is Internet Explorer.
There are many low cost and free applications that will print a directory file list. Some
people use the DOS prompt or a DOS batch file. In this article, I'm going to give you
another method, a small VBScript to run with the Windows Scripting Host (WSH).
The script consists of three functions: ListFolders, ListFiles, and WriteFile. The
ListFolders function is shown below.
Function ListFolders(foldername)
Dim ls, fsObj, fd, fs, fl
ls = ""
Set fsObj = CreateObject("Scripting.FileSystemObject")
Set fd = fsObj.GetFolder(foldername)
Set fs = fd.SubFolders
For Each fl in fs
ls = ls & "[dir] " & fl.name & chr(13) & Chr(10)
Next
ListFolders = ls
Set fsObj = Nothing
End Function
The function takes a folder name as an argument. It creates a FileSystemObject, then passes
the folder name (or more correctly the folder path) to the FileSystemObject's GetFolder method.
The GetFolder method then returns the Subfolders collection. A For Each loop is
used to copy the folder names into a string.
Note how the characters "[dir] " are placed before each folders name, and a carriage-return
chr(13), and linefeed Chr(10) character is placed after each folders name.
The ListFiles function shown below is similar to the ListFolders function.
Function ListFiles(foldername)
Dim ls, fsObj, fd, fs, fl
ls = ""
Set fsObj = CreateObject("Scripting.FileSystemObject")
Set fd = fsObj.GetFolder(foldername)
set fs = fd.Files
For Each fl in fs
ls = ls & fl.name & chr(13) & Chr(10)
Next
ListFiles = ls
Set fsObj = Nothing
End Function
The function takes a folder name as an argument. It creates a FileSystemObject, then passes
the folder name (or more correctly the folder path) to the FileSystemObject's GetFolder method.
The GetFolder method then returns the "Files" collection. A For Each loop is used to copy
the file names into a string.
The WriteFile function shown below takes a folder name (or more correctly the folder
path) as an argument and concatinates the filename dirlist.txt to the path.
Function WriteFile(foldername)
Dim strFile, fsObj, tf, strLines
strFile = foldername & "\dirlist.txt"
Set fsObj = CreateObject("Scripting.FileSystemObject")
Set tf = fsObj.OpenTextFile(strFile, 2, True)
strLines = ListFolders(foldername)
tf.WriteLine strLines
strLines = ListFiles(foldername)
tf.WriteLine strLines
tf.Close
Set fsObj = Nothing
End Function
It creates a FileSystemObject, then uses the FileSystemObject's OpenTextFile method
to create the file "dirlist.txt" in the same folder. It calls ListFolders and ListFiles to
get a list of the subfolders and files in the folder path. These lists are written to the file.
|