Visual Basic Script - Listing Files and Folders Recursively
Author: jason
Date: 2004-05-30
Category: Technical

Here's a quick intro into getting a recursive folder and file listing from VBScript. VBScript is a great way to do little tasks in Windows. This should work on W2K and XP.

What we will do:
1. Get folder input from the user
2. Get the folder, subfolders, file names, and search recursively
3. Write the info to a text file

Here's the full code (list2.vbs)
Code

Option Explicit

Dim FileSys, WrFiles, strInput

strInput= InputBox("Please Enter a starting folder", "User Input","")

Set FileSys = CreateObject("Scripting.FileSystemObject")
Set WrFiles = FileSys.CreateTextFile(strInput & "\files.txt", True)

GetDir strInput

Sub GetDir(dir)
Dim oFile, oFiles
Dim oFolder, oFolders
Dim FolderItem, FileItem

Set oFolder=FileSys.GetFolder(dir)
Set oFolders=oFolder.SubFolders
Set oFiles=oFolder.Files

For each FolderItem in oFolders
GetDir(FolderItem)
Next

WrFiles.WriteLine(oFolder)

FileItem=0
For each FileItem in oFiles
WrFiles.WriteLine(dir & "\" & FileItem.Name)

next
End Sub

WrFiles.Close


This is a little more complicated then the 8 lines from the first lesson, isn't it? Lets step through the code

This sets VBScript into a stricter mode. It will throw up errors if we don't not explicitly set or define something. This makes us code better. :)
Code

Option Explicit


Here we set up our variables. "FileSys" will hold the file system object. "WrLine" writes into our text file. "strInput" will hold the user's input from the popup box.
Code

Dim FileSys, WrFiles, strInput


This will make a message box, and prompt the user to enter a directory/folder to search. The first part is the message inside the window. The second part (separated by commas) is the title bar. The third part is what will appear in the text area. We can put a default path in here if we want, especially if we'll be searching the same dir over and over. Saves our fingers some typing...
Code

strInput= InputBox("Please Enter a starting folder", "User Input","")


The only difference here is that we will place the files.txt into the folder we're looking at. "strInput &" will print out the directory the user specifies into the path where we're saving the file to. It has to be a valid directory though!!
Code

Set FileSys = CreateObject("Scripting.FileSystemObject")
Set WrFiles = FileSys.CreateTextFile(strInput & "\files.txt", True)


This is different this time as well. Here we are going to be calling a sub-routine. The sub is GetDir with the strInput for the folder we want to look in.
Code

GetDir strInput


From here on in it will get a little weird, but hold on. The sub is named GetDir and the (dir) sets the argument to fetch the strInput and hold it. We'll pass it down in a little bit. We also set up some more variables.
Code

Sub GetDir(dir)
Dim oFile, oFiles
Dim oFolder, oFolders
Dim FolderItem, FileItem


We setup oFolder to get the file system object from way above, and open the folder from the argument passed with the sub-routine call. Got that? Basically we are opening the folder the user entered into the popup box here. On the middle line we get the subfolders out of the directory. And on the last line we get the files out.
Code

Set oFolder=FileSys.GetFolder(dir)
Set oFolders=oFolder.SubFolders
Set oFiles=oFolder.Files


Here we step through each item, and call the GetDir sub-routine again to step through the subfolders. And we write out each subfolder name into the text file. Have I lost you yet? Because I'm getting a little dizzy. This is the recursive part.
Code

For each FolderItem in oFolders
GetDir(FolderItem)
Next


WrFiles.WriteLine(oFolder)


Now we do it with the files.
Code

FileItem=0
For each FileItem in oFiles
WrFiles.WriteLine(dir & "\" & FileItem.Name)
next


When finished with everything we can end the sub-routine.
Code

End Sub


Don't forget to close up the text file. Remember it was created the main folder the user typed to search through.
Code

WrFiles.Close


Some of the things in this script were a little confusing. I sometimes get lost on calling a sub-routine, from inside the same sub-routine. And the For each is a little annoying from time to time. This isn't the best or more error free way to write the code, but it is compact.

Next time I'll through in some selective file extension searching, html code, and opening the file after we're done writing to it.

Reference: http://www.scit.wlv.ac.uk/appdocs/chili-asp/html/vbscript_language_reference.htm



jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009