Menu
Script to Print a Directory File List


Learn more at amazon.com

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.

The script uses the code below to present an InputBox to the user.

Dim strPath
strPath = ""
strPath = InputBox("Enter Folder Path: ")
If strPath <> "" And strPath <> VbCancel Then  
  WriteFile(strPath)
End If

If the user enters a folder path (i.e. c:\foldername or C:\foldername\subfoldername), a file named "dirlist.txt" containing a list of the subfolders and files will be created in at that path.

Finish the script by creating a WSH shell at the top of the script and saving the file with the .vbs extension.

Set WShell = WScript.CreateObject("WScript.Shell")

The WSH has a default timeout of 2 seconds. That means the script will close after 2 seconds. That's not enough time to enter a folder path into the InputBox before it disappears. You could configure the WSH to run longer, but a better method is to run the script from a .wsh file (similar to the old PIF file).

Open a text file and enter the text shown below (replace the path with your path and script file name).

[ScriptFile]
Path=C:\scripts\dirlist.vbs

[Options]
Timeout=30

Then save the file with the extension ".wsh". When you double-click on the .wsh file, it will run the script with a 30 second timeout. After you run the script, you can print the dirlist.txt file.

Note: If Windows might not let you open a .wsh file in Notepad. change the file extension to .txt, edit the file, then change the file extension back to .wsh.

Below is the complete code for dirlist.vbs

Set WShell = WScript.CreateObject("WScript.Shell")

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

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

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

Dim strPath
strPath = ""
strPath = InputBox("Enter Folder Path: ")
If strPath <> "" And strPath <> VbCancel Then  
  WriteFile(strPath)
End If


Learn more at amazon.com

More Windows Administration Information:
• Disable Automatic Wireless Configuration in Windows 7
• How to Restore Files From File History in Windows 10
• The Windows 7 Backup and Restore Utility
• Tweaks to Speed Up Internet Streaming Video
• WSH to Master Your Computer
• How to Check Your PCs TPM
• Video - Microsoft Remote Desktop - Part One
• Msconfig - Microsoft's Secret Weapon to Increase Your Computer's Speed
• Computer Technician's Guide to Safety
• Create a Windows 7 Disk Image Backup