ASP Drive Object

Welcome to The Coding College! In this tutorial, we’ll cover the ASP Drive Object, a key component of the FileSystemObject used to interact with and retrieve information about the drives on your server.

Whether you’re managing storage, checking disk space, or organizing server data, the Drive Object provides the tools you need for efficient server-side scripting.

What Is the ASP Drive Object?

The Drive Object is part of the FileSystemObject in Classic ASP and is used to:

  • Retrieve properties of server drives.
  • Check available and total space.
  • Determine the drive type (e.g., fixed, removable, network).

This object is especially useful for applications that rely on dynamic storage management.

How to Use the Drive Object

The Drive Object is accessed through the FileSystemObject using the GetDrive method.

Example:

<%
Dim fso, drive
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set drive = fso.GetDrive("C:")
%>

In this example, the GetDrive method retrieves the properties of the C: drive.

Key Properties of the Drive Object

1. DriveLetter

Returns the letter of the drive.

Syntax:

Drive.DriveLetter

Example:

Response.Write("Drive Letter: " & drive.DriveLetter)

2. DriveType

Returns the type of the drive as an integer:

  • 0: Unknown.
  • 1: Removable.
  • 2: Fixed.
  • 3: Network.
  • 4: CD-ROM.
  • 5: RAM Disk.

Syntax:

Drive.DriveType

Example:

Dim driveType
driveType = drive.DriveType

Select Case driveType
    Case 1
        Response.Write("Drive Type: Removable")
    Case 2
        Response.Write("Drive Type: Fixed")
    Case 3
        Response.Write("Drive Type: Network")
    Case 4
        Response.Write("Drive Type: CD-ROM")
    Case Else
        Response.Write("Drive Type: Unknown")
End Select

3. FreeSpace

Returns the available free space on the drive (in bytes).

Syntax:

Drive.FreeSpace

Example:

Response.Write("Free Space: " & drive.FreeSpace & " bytes")

4. TotalSize

Returns the total size of the drive (in bytes).

Syntax:

Drive.TotalSize

Example:

Response.Write("Total Size: " & drive.TotalSize & " bytes")

5. VolumeName

Returns the volume name of the drive.

Syntax:

Drive.VolumeName

Example:

Response.Write("Volume Name: " & drive.VolumeName)

6. FileSystem

Returns the file system type (e.g., FAT32, NTFS).

Syntax:

Drive.FileSystem

Example:

Response.Write("File System: " & drive.FileSystem)

7. IsReady

Returns True if the drive is ready (e.g., a CD-ROM with a disc inserted).

Syntax:

Drive.IsReady

Example:

If drive.IsReady Then
    Response.Write("The drive is ready.")
Else
    Response.Write("The drive is not ready.")
End If

8. SerialNumber

Returns the serial number of the drive.

Syntax:

Drive.SerialNumber

Example:

Response.Write("Serial Number: " & drive.SerialNumber)

9. AvailableSpace

Returns the space available to the current user (in bytes).

Syntax:

Drive.AvailableSpace

Example:

Response.Write("Available Space: " & drive.AvailableSpace & " bytes")

Practical Examples of the Drive Object

1. Displaying Drive Information

<%
Dim fso, drive
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set drive = fso.GetDrive("C:")

Response.Write("Drive Letter: " & drive.DriveLetter & "<br>")
Response.Write("Total Size: " & drive.TotalSize & " bytes<br>")
Response.Write("Free Space: " & drive.FreeSpace & " bytes<br>")
Response.Write("File System: " & drive.FileSystem & "<br>")
Response.Write("Volume Name: " & drive.VolumeName & "<br>")
%>

2. Checking Disk Space

<%
If drive.FreeSpace < 104857600 Then ' Less than 100 MB
    Response.Write("Warning: Low disk space on drive " & drive.DriveLetter)
End If
%>

3. Listing All Drives on the Server

<%
Dim fso, drives, drive
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set drives = fso.Drives

For Each drive In drives
    Response.Write("Drive: " & drive.DriveLetter & "<br>")
    If drive.IsReady Then
        Response.Write("Total Size: " & drive.TotalSize & " bytes<br>")
        Response.Write("Free Space: " & drive.FreeSpace & " bytes<br>")
    Else
        Response.Write("Drive not ready.<br>")
    End If
    Response.Write("<br>")
Next
%>

Error Handling in Drive Operations

Always handle potential errors, such as inaccessible drives or insufficient permissions.

Example:

<%
On Error Resume Next

Dim fso, drive
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set drive = fso.GetDrive("Z:") ' Non-existent or network drive

If Err.Number <> 0 Then
    Response.Write("Error accessing drive: " & Err.Description)
    Err.Clear
Else
    Response.Write("Drive Letter: " & drive.DriveLetter)
End If

On Error GoTo 0
%>

Best Practices

  1. Validate Input:
    • When accepting drive letters dynamically, validate the input to avoid potential issues.
  2. Error Handling:
    • Implement robust error handling for scenarios like missing drives or insufficient permissions.
  3. Monitor Disk Space:
    • Use the Drive Object to monitor disk usage and alert administrators when space runs low.
  4. Security:
    • Ensure proper server security configurations to prevent unauthorized access to drive data.

Conclusion

The ASP Drive Object is a versatile tool for accessing and managing drive-level information in Classic ASP. From checking disk space to retrieving file system details, the Drive Object simplifies server-side scripting tasks.

For more tutorials and coding tips, visit The Coding College, your trusted resource for mastering programming concepts!

Leave a Comment