Welcome to Bucaro TecHelp!

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 RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022


List Drives and Folders with the VBScript FileSystemObject

Scripts are (usually) small interpreted (meaning they don't get compiled) programs that you can write using a simple text editor like Windows Notepad. VBScript stands for "Visual Basic Script", which means it's written in Microsoft's easy to understand Visual Basic language. The scripts are interpreted and executed by a program called the "Windows Scripting Host", (WSH) which is installed on Windows computers by default.

It's easy to run a VBScript in the WSH, all you have to do is give the text file containing the code a the .vbs file extension, then double-click on the file name. When you run a VBScript it has access to the VBScript runtime objects.

To access the computer's file system, you must create a FileSystemObject with the CreateObject method:

Set fsObj = CreateObject("Scripting.FileSystemObject")

With the FileSystemObject you can you can create a Drives collection object:

Set colDrives = fsObj.Drives

With a For Each Next loop, you can loop through all the drives on the computer and access each drive's properties. In the example shown below, we access the drive's letter:

For Each objDrive in colDrives
    ls = ls & objDrive.DriveLetter & chr(13) & Chr(10)
Next

When you have a folder object, you can access its subfolder collection. But a drive object doesn't have a subFolder property. Instead, it has RootFolder property which you can pass to the GetFolder method, and then access the RootFolder's SubFolders object:

For Each objDrive in colDrives
    ls = ls & objDrive.DriveLetter & chr(13) & Chr(10)
    For Each objFolder In fsObj.GetFolder(objDrive.RootFolder).SubFolders
    Next
Next

Shown below is a complete script that you can use to list drives and their top-level folders:

Dim fsObj, colDrives, objDrive, objFolder, tf, ls

On Error Resume Next
Set fsObj = CreateObject("Scripting.FileSystemObject")
Set colDrives = fsObj.Drives

ls =""
For Each objDrive in colDrives
    ls = ls & objDrive.DriveLetter & chr(13) & Chr(10)
    For Each objFolder In fsObj.GetFolder(objDrive.RootFolder).SubFolders
         ls = ls & objFolder.Name & chr(13) & Chr(10)
    Next
Next

Set tf = fsObj.OpenTextFile("drvfldr.txt", 2, True, True)
tf.WriteLine ls

tf.Close
Set fsObj = Nothing

Note that I have added the On Error Resume Next statement because as you loop through all the folders on all the drives you will inevitably be attempting to access folders that you are not authorized to access, causing an error.

Note that I've also used the FileSystemObject's OpenTextFile method to create a file named "drvfldr.txt" to which the script will write the information it collects.

To use the script, copy and paste the code above into a text file and save it with a file name with the extension .vbs. When you double-click on the filename to execute the script, it will write the file "drvfldr.txt" containing a list of your computer's drives and their top-level folders.

RSS Feed RSS Feed


Follow Stephen Bucaro Follow @Stephen Bucaro


Fire HD
[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyrightę2001-2010 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269