Get drive information (Disk ID, FreeSpace, Label, etc)
Posted: 2002-06-01
By: ArchiveBot
Viewed: 211
Filed Under:
VB6 Code Cache, VB6 Files and I/O, VB6 Custom Functions, VB6 Miscellaneous, VB6 System Services, VB6 Windows API
No attachments for this post
These functions will provide you quick, fail-safe information about a specific disk letter you specify (ex: "C:" )
GetDriveSerialID - Returns the serial number of a drive partition (if available)
GetDriveFreeSpace - Returns the free space of the specified drive (if available)
GetDriveSize - Returns the total drive space (if available)
GetDriveUsedSpace - Returns the used disk space of the specified drive (if available)
GetDriveLabel - Returns the volume label of the specified drive
Some functions are declared as variant instead of longs, avoiding the 2GB limit.
Original Author: Filipe Lage
Code
I know that this isn't new, and this information could be optained in many different ways (using API, etc).
I'm just providing this info for beginers.
Instructions:
Put this code in a new form and add one button (named "Command1")
------------ ~ --------------
Public Function GetDriveSerialID(diskletter As String) As String
On Error Resume Next
Set c = CreateObject("scripting.filesystemobject")
GetDriveSerialID = Hex(c.drives(Left(diskletter, 1)).serialnumber)
Set c = Nothing
End Function
Public Function GetDriveFreeSpace(diskletter As String) As Variant
On Error Resume Next
Set c = CreateObject("scripting.filesystemobject")
GetDriveFreeSpace = 0 ' default
GetDriveFreeSpace = c.drives(Left(diskletter, 1)).freespace
Set c = Nothing
End Function
Public Function GetDriveSize(diskletter As String) As Variant
On Error Resume Next
Set c = CreateObject("scripting.filesystemobject")
GetDriveSize = 0 ' default
GetDriveSize = c.drives(Left(diskletter, 1)).totalsize
Set c = Nothing
End Function
Public Function GetDriveUsedSpace(diskletter As String) As Variant
On Error Resume Next
GetDriveUsedSpace = GetDriveSize(diskletter) - GetDriveFreeSpace(diskletter)
End Function
Public Function GetDriveLabel(diskletter As String) As String
On Error Resume Next
Set c = CreateObject("scripting.filesystemobject")
GetDriveLabel = c.drives(Left(diskletter, 1)).volumename
Set c = Nothing
End Function
Private Sub Command1_Click()
UseHD = "C:"
Debug.Print "Volume Label: " & GetDriveLabel("c")
Debug.Print "Disk serial number: " & GetDriveSerialID("c")
Debug.Print "Free space: " & GetDriveFreeSpace("s") & " bytes"
Debug.Print "Total drive size: " & GetDriveSize("c") & " bytes"
Debug.Print "Total used space: " & GetDriveUsedSpace("c") & " bytes"
End Sub
------------ ~ --------------
Vote if you wish. I appreciate it
// FCLage
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.