Obtaining Drive Information

Published on April 5, 2010

The Windows Script Host Object Model has many useful properties and methods for dealing with files, folders, disks, etc. One common use is for checking the amount of available space on a drive before creating a large file. The example function below illustrates how to use the FileSystemObject along with the Drives, and Drive objects to iterate through all drives on your system.

To use this example in your macros you must add a reference to the Windows Script Host Object Model library. To do this select Tools > References, then scroll toward the bottom of the list and select Windows Script Host Object Model.

'
'This function requires a reference to: 
'Windows Script Host Object Model (C:\Windows\System32\wshom.ocx)"
'
Public Sub MyMacro() 
  Dim objFSO As FileSystemObject
  Dim objDrive As Drive
    
  'Set new instance if File System Object 
  Set objFSO = New FileSystemObject
  
  'A UserForm with a ListBox control has been setup 
  'to display the results
  With UserForm1
    'Iterate through all drives 
    For Each objDrive In objFSO.Drives
      .ListBox1.AddItem "Drive – " & objDrive.DriveLetter & ":"
  
     'If the drive is ready get the information 
     'Size and space values are returned in bytes 
      If objDrive.IsReady Then
        .ListBox1.AddItem "Free Space = " & cstr(objDrive.FreeSpace)
        .ListBox1.AddItem "Avail Space = " & cstr(objDrive.AvailableSpace)
        .ListBox1.AddItem "Total Size = " & cstr(objDrive.TotalSize)
      Else
        .ListBox1.AddItem "Drive is not ready"
      End If

      .ListBox1.AddItem ""
    Next

    .Show
  
  End With
End Sub

Tags:
Like this article? Share it!

One Response to “Obtaining Drive Information”

  1. Envision Newsletter April 2010 - Envision CAD | MicroStation Training & InRoads Training Says:

    April 11th, 2011 at 4:51 pm

    […] VBA: Obtaining Drive Information […]

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Privacy Policy