DriveObject Guide: Implementing Sub ShowFreeSpace(drvPath)

Written by

in

DriveObject Guide: Implementing Sub ShowFreeSpace(drvPath) When managing system resources via VBScript or Visual Basic for Applications (VBA), interacting with drives is a common requirement. The DriveObject within the FileSystemObject (FSO) library provides an efficient way to retrieve information about local or network drives.

This guide explains how to implement the Sub ShowFreeSpace(drvPath) procedure to check available disk space, a crucial task for monitoring system health and preventing application errors caused by low storage. Prerequisites: The FileSystemObject

To work with drives, you must first create an instance of the Scripting.FileSystemObject. This library allows you to access the Drive object, which provides properties like FreeSpace, VolumeName, and DriveLetter. Implementing Sub ShowFreeSpace(drvPath)

The following VBA/VBScript subroutine takes a drive path (e.g., “C:”) as input and displays its available space in kilobytes.

Sub ShowFreeSpace(drvPath) Dim fs, d, s ‘ 1. Create the FileSystemObject Set fs = CreateObject(“Scripting.FileSystemObject”) ’ 2. Get the Drive object based on the provided path Set d = fs.GetDrive(fs.GetDriveName(drvPath)) ‘ 3. Gather Drive Information s = “Drive ” & UCase(drvPath) & “ - ” s = s & d.VolumeName & vbCrLf ’ 4. Calculate Free Space (converting from bytes to KB) s = s & “Free Space: ” & FormatNumber(d.FreeSpace / 1024, 0) s = s & “ Kbytes” ‘ 5. Display the result MsgBox s End Sub Use code with caution. Detailed Breakdown

CreateObject(“Scripting.FileSystemObject”): Initializes the FSO library.

fs.GetDrive(…): Accesses the specific Drive object needed for retrieval. d.VolumeName: Retrieves the label of the drive.

d.FreeSpace: Retrieves the amount of free space (in bytes) available to the user.

FormatNumber(…, 0): Formats the calculation to make it readable without decimal places. FreeSpace vs. AvailableSpace

While FreeSpace is excellent for general reporting, the Drive object also supports AvailableSpace. FreeSpace: The total free space available on the drive.

AvailableSpace: The amount of space available to the user, which may differ from FreeSpace on systems that support disk quotas. Key DriveObject Properties

Beyond FreeSpace, the Drive object enables you to retrieve comprehensive system information: DriveLetter: Returns the drive letter (e.g., “C”).

DriveType: Identifies if the drive is Removable, Fixed, Network, CD-ROM, or RAM Disk.

FileSystem: Returns the type of file system (FAT, FAT32, NTFS).

IsReady: Returns a boolean (True/False) indicating if the drive is available. TotalSize: Returns the total size of the drive in bytes. Example Usage You can call the subroutine in your code like this:

’ Example for local drive ShowFreeSpace “C:” ‘ Example for network drive ShowFreeSpace “D:” Use code with caution.

Using the ShowFreeSpace procedure allows you to easily incorporate drive monitoring into your automation scripts. If you want, I can:

Explain how to change the output to Megabytes (MB) or Gigabytes (GB)

Show you how to add error handling to skip drives that aren’t ready Show how to export the output to a text file Let me know how you’d like to expand this script. FreeSpace property (Visual Basic for Applications)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *