VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Get drive information (Disk ID, FreeSpace, Label, etc)

by Filipe Lage (7 Submissions)
Category: Files/File Controls/Input/Output
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

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.

Rate Get drive information (Disk ID, FreeSpace, Label, etc)

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

Download this snippet    Add to My Saved Code

Get drive information (Disk ID, FreeSpace, Label, etc) Comments

No comments have been posted about Get drive information (Disk ID, FreeSpace, Label, etc). Why not be the first to post a comment about Get drive information (Disk ID, FreeSpace, Label, etc).

Post your comment

Subject:
Message:
0/1000 characters