This article shows you how to list all the drives in a computer system
using the FileSystemObject. The article shows how to create a sample
project that lists all the drives in a system as well as the properties of
This article assumes you are familiar with using objects, collections, and
object models in Visual Basic. The FileSystemObject allows you to perform a
number tasks on the file system of a computer. This object is available to
your project by making a reference to the Microsoft Script Runtime file
scrrun.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 instance
of the FileSystemObject using the CreateObject method or by creating a
variable as a new FileSystemObject. The object allows you to access each
Drive object in the Drives collection. Each Drive has a number of
properties 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 all
the drives in a system and the properties of each drive.
- Start a new Standard EXE project in Visual Basic. Form1 is created by
- Add a reference to the Microsoft Script Runtime by completing the
- From the Project menu, click References to display the References
- 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:
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
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"
sDrive = sDrive & sDriveType
Set fsoDrives = Nothing
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)
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: " & _
Label1.Caption = sDriveSpec
MsgBox ("Drive Not Ready")
Set fsoDrive = Nothing
- 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 the
Microsoft Knowledge Base:
: How To Use FileSystemObject with Visual Basic
: How To Recursively Search Directories Using FileSystemObject
Article ID: 189667 - Last Review: February 21, 2014 - Revision: 2.1
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
|kbnosurvey kbarchive kbhowto kbctrl KB189667|