This article was previously published under Q189667
This article has been archived. It is offered "as is" and will no longer be updated.
This article shows you how to list all the drives in a computer systemusing the FileSystemObject. The article shows how to create a sampleproject that lists all the drives in a system as well as the properties ofeach drive.
This article assumes you are familiar with using objects, collections, andobject models in Visual Basic. The FileSystemObject allows you to perform anumber tasks on the file system of a computer. This object is available toyour project by making a reference to the Microsoft Script Runtime filescrrun.dll. This file ships with Windows 98 and the following products:
Windows Script Host Windows NT Option Pack Microsoft Internet Information Server 3.0 Scripting 3.1 upgrade Visual Studio 6.0 Visual Basic 6.0
After making a reference to the Script Runtime file, you create an instanceof the FileSystemObject using the CreateObject method or by creating avariable as a new FileSystemObject. The object allows you to access eachDrive object in the Drives collection. Each Drive has a number ofproperties that you can query to determine the drive type, the total space,the free space, and the file system.
The next section shows you how to create a sample project that displays allthe drives in a system and the properties of each drive.
Start a new Standard EXE project in Visual Basic. Form1 is created by default.
Add a reference to the Microsoft Script Runtime by completing the following steps:
From the Project menu, click References to display the References dialog box.
Click Browse to open the Add Reference dialog box.
Select the file scrrun.dll and click OK to close the Add Reference dialog box. This file is installed in your system directory. Microsoft Scripting Runtime appears with a check in the Available Referenced list box of the References dialog box.
Click OK to close the References dialog box.
Add a CommandButton, ListBox, and Label to Form1.
Copy the following code to the Code window of Form1:
Option Explicit Dim fso As New FileSystemObject Dim fsoDrives As Drives Dim fsoDrive As Drive Private Sub Form_Load() Label1.AutoSize = True Command1.Caption = "List All Drives" List1.Enabled = False End Sub Private Sub Command1_Click() Dim sDrive As String Dim sDriveType As String Set fsoDrives = fso.Drives List1.Enabled = True For Each fsoDrive In fsoDrives sDrive = "Drive " & fsoDrive.DriveLetter & ": " Select Case fsoDrive.DriveType Case 0: sDriveType = "Unknown" Case 1: sDriveType = "Removable Drive" Case 2: sDriveType = "Fixed Disk" Case 3: sDriveType = "Remote Disk" Case 4: sDriveType = "CDROM Drive" Case 5: sDriveType = "RAM Disk" End Select sDrive = sDrive & sDriveType List1.AddItem (sDrive) Next Set fsoDrives = Nothing End Sub Private Sub List1_Click() Dim sDriveSpec As String Dim sSelDrive As String sSelDrive = List1.List(List1.ListIndex) sSelDrive = Mid(sSelDrive, 7, 1) Set fsoDrive = fso.GetDrive(sSelDrive) With fsoDrive If .IsReady = True Then sDriveSpec = "Drive " & .DriveLetter & _ " Specifications" & vbLf sDriveSpec = sDriveSpec & "Free Space: " & _ .FreeSpace & " bytes" & vbLf sDriveSpec = sDriveSpec & "File System: " & _ .FileSystem & vbLf sDriveSpec = sDriveSpec & "Serial Number: " & _ .SerialNumber & vbLf sDriveSpec = sDriveSpec & "Total Size: " & _ .TotalSize & " bytes" & vbLf sDriveSpec = sDriveSpec & "Volume Name: " & _ .VolumeName Label1.Caption = sDriveSpec Else MsgBox ("Drive Not Ready") End If End With Set fsoDrive = Nothing End Sub
On the Run menu, select Start, or press the F5 key to start the program. Click the "List All Drives" button to display all the drives in your system in the ListBox. Click on a drive in the ListBox to display the properties of the drive.
For additional information, please see the following articles in theMicrosoft Knowledge Base:
186118 : How To Use FileSystemObject with Visual Basic
185601 : How To Recursively Search Directories Using FileSystemObject