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 Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact Advertise on Bucaro TecHelp Advertise Here RSS News Feeds News Feeds


Computer Repair with Diagnostic Flowcharts

Troubleshoot PC Hardware Problems With Flowcharts

This manual for troubleshooting PC hardware problems creates a visual expert system for diagnosing component failure and identifying conflicts.

The seventeen diagnostic flowcharts at the core of this book are intended for the intermediate to advanced hobbyist, or the beginning technician.

Click Here

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.

RSS Feed RSS Feed



Computer Sections

Windows

Here's the information you need to not only prepare for the MCITP certification exams as a Windows 7 desktop support technician or administrator, but also to excel in your job.

From successfully troubleshooting individual desktops to planning and configuring Windows 7 desktop infrastructures on a broad scale, this reference explores the real-world tasks and scenarios you'll face on the job and shows you step by step how to handle them.

Reader Bill Talbott says, "The author has an innate ability to break out complex and often boring topics and make them interesting to the end reader. His writing style, combined with the exercises throughout this text, ensure complete understanding of each topic...

Click here for more information.


TigerDirect
[Site User Agreement] [Advertise on This site] [Search This Site] [Contact Form]
Copyright©2001-2011 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268