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.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Script to Print a Directory File List

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.

Computer Sections

RSS Feed RSS Feed

Windows System Administration
Introduction to DOS
DOS Switches and Wild Cards
Installing a Local Printer on Windows Vista
Installing a Network Printer on Windows XP and Vista
SMART Disk Drives Warn You Before They Fail
Is Your Critical PC Data Adequately Protected From Disaster?
Security Risks and Ways to Decrease Vulnerabilities in a 802.11b Wireless Environment
Configure Vista's Data Execution Prevention
Internet Connection Sharing in Windows XP
How to Backup Mails and Address Book of Outlook Express
Windows 2000 Security Overview
Kill The Messenger (Service)
Use the HOSTS File to Block Web Sites
FreeDOS
A Day in the Life of a System Administrator
Microsoft Licensing Explained
Guide To Setting Up Dual Monitors
Windows 7 Tweaks
Hands-On Microsoft Windows Server 2008 Administration
Disable Indexing to Speed Up Your Computer
Configure Windows Indexing Service for Performance
Configuring Windows as a NTP (Network Time Protocol) Server
Font Basics
Three Important Techniques for Securing a Wireless Network
Defend Your Business with a Firewall
Msconfig - Microsoft's Secret Weapon to Increase Your Computer's Speed
Choosing a Tape Drive
How to Remove the Bloat from Your PC
Create a Windows 7 System Repair Disc
Computer Data Backups - Test Now or Cry Later
PC Technician Certifications and Professional Organizations
PC Technician's Guide to Providing Telephone Support
PC Technician's Software Copyright Responsibilities
CompTIA A+ Complete Study Guide
Hard Disk Management
Windows Server 2003 Active Directory and Network Infrastructure
The Security and Maintenance of Messages in Outlook Express
Planning a Backup and Restoration of Files for Disaster Recovery
WSH to Master Your Computer
Script to Print a Directory File List
Disable Long Filenames to Improve Window's Performance
Disable Kernal Paging to Speed Up Windows
Script to Display the Processes Running on a Computer
Script to Identify Your Systems HAL
To Protect Your PC Disable the Windows Scripting Host


TigerDirect
[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269