How do you check for available diskspace?

I'm sure this is a no-brainer, but I don't find anything doing an index search in MSDN or in the WinAPI stuff...thanks.
LVL 1
DreamingEagleAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
The "Total Space" property is just for consistency .TotalSize!

Here is the full list of properties of the drive object.

AvailableSpace Property
DriveLetter Property
DriveType Property
FileSystem Property
FreeSpace Property
IsReady Property
Path Property
RootFolder Property
SerialNumber Property
ShareName Property
TotalSize Property
VolumeName Property

0
 
TimCotteeHead of Software ServicesCommented:
Here is one way using the scripting runtime - FileSystemObject:

Private Sub Command1_Click()
    Dim fs, d, s
    drvPath = "C:\"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set d = fs.GetDrive(fs.GetDriveName(drvPath
    s = "Drive " & UCase(drvPath) & " - "
    s = s & d.VolumeName & vbCrLf
    s = s & "Available Space: " & FormatNumber(d.AvailableSpace / 1024, 0)
    s = s & " Kbytes"
    MsgBox s
End Sub

0
 
AzraSoundCommented:
another way, little more elaborate using api:


'GET DISK FREE SPACE AND TOTAL DISK SPACE
'ADD A COMMAND BUTTON, TWO LABELS, AND A DRIVELISTBOX TO THE FORM
'***********************************************************************
'
'MODULE CODE
'
'***********************************************************************

Public Type ULong ' Unsigned Long
    Byte1 As Byte
    Byte2 As Byte
    Byte3 As Byte
    Byte4 As Byte
End Type


Public Type LargeInt ' Large Integer
    LoDWord As ULong
    HiDWord As ULong
    LoDWord2 As ULong
    HiDWord2 As ULong
End Type


Public Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" _
    (ByVal lpRootPathName As String, FreeBytesAvailableToCaller As LargeInt, _
    TotalNumberOfBytes As LargeInt, TotalNumberOfFreeBytes As LargeInt) As Long



'***********************************************************************
'
'FORM CODE
'
'***********************************************************************


Function GetFreeSpace1(strPath As String) As Double
    Dim nFreeBytesToCaller As LargeInt
    Dim nTotalBytes As LargeInt
    Dim nTotalFreeBytes As LargeInt
   
    strPath = Trim(strPath)


    If Right(strPath, 1) <> "\" Then
        strPath = strPath & "\"
    End If
   


    If GetDiskFreeSpaceEx(strPath, nFreeBytesToCaller, nTotalBytes, nTotalFreeBytes) <> 0 Then
        GetFreeSpace1 = CULong( _
        nFreeBytesToCaller.HiDWord.Byte1, _
        nFreeBytesToCaller.HiDWord.Byte2, _
        nFreeBytesToCaller.HiDWord.Byte3, _
        nFreeBytesToCaller.HiDWord.Byte4) * 2 ^ 32 + _
        CULong(nFreeBytesToCaller.LoDWord.Byte1, _
        nFreeBytesToCaller.LoDWord.Byte2, _
        nFreeBytesToCaller.LoDWord.Byte3, _
        nFreeBytesToCaller.LoDWord.Byte4)
    End If
End Function

Function GetFreeSpace2(strPath As String) As Double
    Dim nFreeBytesToCaller As LargeInt
    Dim nTotalBytes As LargeInt
    Dim nTotalFreeBytes As LargeInt
   
    strPath = Trim(strPath)


    If Right(strPath, 1) <> "\" Then
        strPath = strPath & "\"
    End If
   


    If GetDiskFreeSpaceEx(strPath, nFreeBytesToCaller, nTotalBytes, nTotalFreeBytes) <> 0 Then
        GetFreeSpace2 = CULong( _
        nTotalBytes.HiDWord.Byte1, _
        nTotalBytes.HiDWord.Byte2, _
        nTotalBytes.HiDWord.Byte3, _
        nTotalBytes.HiDWord.Byte4) * 2 ^ 32 + _
        CULong(nTotalBytes.LoDWord.Byte1, _
        nTotalBytes.LoDWord.Byte2, _
        nTotalBytes.LoDWord.Byte3, _
        nTotalBytes.LoDWord.Byte4)
    End If
End Function


Function CULong(Byte1 As Byte, Byte2 As Byte, Byte3 As Byte, Byte4 As Byte) As Double
    CULong = Byte4 * 2 ^ 24 + Byte3 * 2 ^ 16 + Byte2 * 2 ^ 8 + Byte1
End Function


Private Sub Command1_Click()
    Dim ret As Double
    Dim ret2 As Double
    ret = GetFreeSpace1(Drive1.Drive & "\")
    ret2 = GetFreeSpace2(Drive1.Drive & "\")
    Label1.Caption = Label1.Caption & SizeString(ret)
    Label2.Caption = Label2.Caption & SizeString(ret2)
End Sub

Private Function SizeString(ByVal num_bytes As Double) As String
    Const SIZE_KB As Double = 1024
    Const SIZE_MB As Double = 1024 * SIZE_KB
    Const SIZE_GB As Double = 1024 * SIZE_MB
    Const SIZE_TB As Double = 1024 * SIZE_GB
    Dim Size$

    If num_bytes < SIZE_KB Then
        Size$ = Format$(num_bytes) & " bytes"
    ElseIf num_bytes < SIZE_MB Then
        Size$ = Format$(num_bytes / SIZE_KB, "0.00") & " KB"
    ElseIf num_bytes < SIZE_GB Then
        Size$ = Format$(num_bytes / SIZE_MB, "0.00") & " MB"
    Else
        Size$ = Format$(num_bytes / SIZE_GB, "0.00") & " GB"
    End If
    SizeString = Size$
End Function

Private Function LargeIntegerToDouble(low_part As Long, high_part As Long) As Double
    Dim ret As Double

    ret = high_part
    If high_part < 0 Then ret = ret + 2 ^ 32
    ret = ret * 2 ^ 32

    ret = ret + low_part
    If low_part < 0 Then ret = ret + 2 ^ 32

    LargeIntegerToDouble = ret
End Function

Private Sub Form_Load()
    Command1.Caption = "Get Disk Info"
    Label1.Caption = "Disk Free Space: "
    Label2.Caption = "Total Disk Space: "
   
End Sub


0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
p_biggelaarCommented:
Check again:

You can use GetDiskFreeSpace or GetDiskFreeSpaceEx. Both entries can be found in the Index of the MSDN library if you want to know more about their usage. This is how you should declare the function:

Public Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long

Or:

Public Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceB" (ByVal lpDirectoryName As String, lpFreeBytesAvailable As Long, lpTotalNumberOfBytes As Long, lpTotalNumberOfFreeBytes As Long) As Long

In your code you could place a call like this:
Dim blnTemp as boolean
Dim lngAvailable as long
Dim lngTotalBytes as long
Dim lngTotalFreeBytes as long
Dim strDir

strDir = "C:\"
blnTemp = GetDiskFreeSpaceB(strDir, lngAvailable, lngTotalBytes, lngTotalFreeBytes)

If blnTemp = True Then
  msgbox "You have " & lngAvailable & " bytes available in " & strDir
End If

Good luck!
0
 
wsh2Commented:
Here is how you do it with FileSystemObjects.. <smile>.

1. Add a reference (VB Menu -> Project -> References) to the "Microsoft Scripting Runtime" (SCRRUN.DLL).
2. Copy and Paste the following function into your code.

Usage Syntax Examples:
 Dim lngDriveSpace As Long
 lngDriveSpace = xGetDriveSpace("C")
 lngDriveSpace = xGetDriveSpace("D:")
 lngDriveSpace = xGetDriveSpace("E:\")
 lngDriveSpace = xGetDriveSpace("\\Net1\C\")

<----- Code Begin ----->

Private Function xGetDriveSpace(ByVal strDrive As String) As Long
   
   Dim fsoWork As FileSystemObject
   Dim fsoDrive As Drive
   Set fsoWork = CreateObject("Scripting.FileSystemObject")
   Set fsoDrive = fsoWork.GetDrive(strDrive)
   xGetDriveSpace = FormatNumber(fsoDrive.AvailableSpace / 1024, 0)
   
End Function
 
0
 
wsh2Commented:
<laughing>.. It just doesn't pay to be a slow typist.. <smile>
0
 
DreamingEagleAuthor Commented:
Works great, Tim.  I'll add another 10 points if you will tell me the name of the "Total Space" property.  

I should have said I want to show a pie chart of how much space is on the drive, how much is available, and how much my data intensive application is using....

Thanks, also, Azra, but I like the FileSystemObject stuff a little better.  Trouble with it is I find nothing in the way of examples for using FileSystemObject, but that's probably not having enough time to look harder...:)



0
 
DreamingEagleAuthor Commented:
Thanks, Tim, I took that info and created Function GetDriveInfo() and put it in my library so I don't ever have to do this again...:)



Private Sub ShowDiskInfo

    msgbox GetDriveInfo("C", "TotalSize")
    msgbox GetDriveInfo("C", "UsedSpace")
    msgbox GetDriveInfo("C", "AvailableSpace")
    msgbox GetDriveInfo("C", "DriveType")
    msgbox GetDriveInfo("C", "Path")
    msgbox GetDriveInfo("C", "RootFolder")
    msgbox GetDriveInfo("C", "FileSystem")
    msgbox GetDriveInfo("C", "FreeSpace")
    msgbox GetDriveInfo("C", "IsReady")
    msgbox GetDriveInfo("C", "Path")
    msgbox GetDriveInfo("C", "RootFolder")
    msgbox GetDriveInfo("C", "SerialNumber")
    msgbox GetDriveInfo("C", "ShareName")
    msgbox GetDriveInfo("C", "VolumeName")
    msgbox GetDriveInfo("C", "DriveDescription")
end sub

Public Function GetDriveInfo(sDriveLetter As String, sItemToReturn) As Variant
    Dim FileSystemObj, objDrive
   
    Dim lngTotalSize As Long
    Dim lngAvailableSpace As Long
    Dim lngUsedSpace As Long
    Dim lngFreeSpace As Long
    Dim vDriveType As Variant
    Dim sFileSystem As String
    Dim bIsReady As Boolean
    Dim sPath As String
    Dim sRootFolder As String
    Dim vSerialNumber As Variant
    Dim sShareName As String
    Dim sVolumeName As String
    Dim sDesc As String
   
    sDriveLetter = sDriveLetter & ":\"
   
    Set FileSystemObj = CreateObject("Scripting.FileSystemObject")
    Set objDrive = FileSystemObj.GetDrive(FileSystemObj.GetDriveName(sDriveLetter))
   
    With objDrive
        lngTotalSize = .totalsize
        lngAvailableSpace = .AvailableSpace
        vDriveType = .drivetype
        bIsReady = .isready
        sPath = .Path
        sRootFolder = .rootfolder
        vSerialNumber = .serialnumber
        sShareName = .sharename
        sVolumeName = .volumename
    End With
    lngUsedSpace = lngTotalSize - lngAvailableSpace
   
    sDesc = "Drive: " & sDriveLetter & vbCrLf & _
            "Volume: " & sVolumeName & vbCrLf & _
            "Path: " & sPath & vbCrLf & _
            "Root Folder: " & sRootFolder & vbCrLf & _
            "Serial Number: " & vSerialNumber & vbCrLf & _
            "Share Name: " & sShareName & vbCrLf & _
            "Is Drive Ready: " & IIf(bIsReady, "Yes", "No") & vbCrLf & _
            "Total Size: " & Format(lngTotalSize, "#,###") & vbCrLf & _
            "Used Space: " & Format(lngUsedSpace, "#,###") & vbCrLf & _
            "Available Space: " & Format(lngAvailableSpace, "#,###")
           
    Select Case sItemToReturn
        Case "TotalSize"
            GetDriveInfo = sDriveLetter
        Case "UsedSpace"
            GetDriveInfo = lngUsedSpace
        Case "AvailableSpace"
            GetDriveInfo = lngAvailableSpace
        Case "DriveType"
            GetDriveInfo = vDriveType
        Case "Path"
            GetDriveInfo = sPath
        Case "RootFolder"
            GetDriveInfo = sRootFolder
        Case "FileSystem"
            GetDriveInfo = sFileSystem
        Case "FreeSpace"
            GetDriveInfo = lngFreeSpace
        Case "IsReady"
            GetDriveInfo = bIsReady
        Case "Path"
            GetDriveInfo = sPath
        Case "RootFolder"
            GetDriveInfo = sDriveLetter
        Case "SerialNumber"
            GetDriveInfo = vSerialNumber
        Case "ShareName"
            GetDriveInfo = sDriveLetter
        Case "VolumeName"
            GetDriveInfo = sVolumeName
        Case "DriveDescription"
            GetDriveInfo = sDesc
        Case Else
            GetDriveInfo = "Error"
    End Select
End Function
0
 
DreamingEagleAuthor Commented:
Thanks, also, to the rest of you.  The high quality of answers I get on this forum---and the quickness of the response---never ceases to amaze me...
0
 
DreamingEagleAuthor Commented:
Sorry, Tim, I forgot to add the exta ten points like I promised I would.  I'll catch you next time, though.
0
 
DreamingEagleAuthor Commented:
Sorry, Tim, I forgot to add the exta ten points like I promised I would.  I'll catch you next time, though.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.