XTreme Logo

If you appreciate these tips, please consider a small donation:

General Tip - Intermediate

How to Determine Drive Size and Available Space Even On Huge Drives

I needed to know the drive size of a system and its amount of free space.
It is much more difficult now with the HUGE drives that are available.
This is what I came up with...
 
Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This sample uses one form with one command button and 
' three text boxes.
' Type the drive letter(or UNC) you want to know the size
' of into Text1.
' Text2 returns the total number of bytes on the drive.
' Text3 returns the total number of free bytes on the drive. 
' When using an API call that expects an unsigned long
' integer you need to pass it a currency datatype to capture
' the value because VB does not have an unsigned long.
' However the currency will have the value and can be 
' converted once returned.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
' Dimension API call.
Public Declare Function GetDiskFreeSpaceEx Lib "kernel32" _
          Alias "GetDiskFreeSpaceExA" _
          (ByVal lpDirectoryName As String, _
          lpFreeBytesAvailableToCaller As Currency, _
          lpTotalNumberOfBytes As Currency, _
          lpTotalNumberOfFreeBytes As Currency) As Long
' Dimension Constant values.
Const lGigaByte As Long = 1073741824
Const lMegaByte As Long = 1048576 
Public Sub Command1_Click()
    ' Dimension local variables.
     Dim cJunkValue As Currency
     Dim cTotalNumberOfBytesOnDrive As Currency
     Dim cTotalNumberOfFreeBytes As Currency
     Dim lResults As Long
     lResults = GetDiskFreeSpaceEx(Text1.Text, cJunkValue, _
                    cTotalNumberOfBytesOnDrive, cTotalNumberOfFreeBytes) 
     ' Format and Display the TotalNumberOfBytesOnDrive value in
     ' GB or MB depending on size.
     ' Multiply the TotalNumberOfBytesOnDrive value by 10000 to
     ' convert the currency data into a long.
     Text2.Text = Format _
          ((cTotalNumberOfBytesOnDrive * 10000) / _
          IIf(cTotalNumberOfBytesOnDrive * 10000 >= lGigaByte, _
          lGigaByte, lMegaByte), IIf(cTotalNumberOfBytesOnDrive * _
         10000 >= lGigaByte, "##0.###GB", "##0.###MB")) 
     ' Format and Display the TotalNumberOfFreeBytes value in
     ' GB or MB depending on size. Remember 1 Gigabyte 1024 Megabytes.
     ' Multiply the TotalNumberOfFreeBytesvalue by 10000 to convert
     ' the currency data into a long.
     Text3.Text = Format _
          ((cTotalNumberOfFreeBytes * 10000) / IIf(cTotalNumberOfFreeBytes * _
            10000 >= lGigaByte, lGigaByte, lMegaByte), _
             IIf(cTotalNumberOfFreeBytes * 10000 >= lGigaByte, "##0.###GB", _
            "##0.###MB")) 
End Sub
 
 

[ Back To The Top ]

Contact: web@xtremecomp.com
All contents copyright XTreme Computing unless noted otherwise.