Solved

How do you check for available diskspace?

Posted on 2000-04-18
11
172 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:DreamingEagle
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 2725877
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2725886
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
 
LVL 2

Expert Comment

by:p_biggelaar
ID: 2725897
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2725907
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2725914
<laughing>.. It just doesn't pay to be a slow typist.. <smile>
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:DreamingEagle
ID: 2725915
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
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 2725938
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
 
LVL 1

Author Comment

by:DreamingEagle
ID: 2735496
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
 
LVL 1

Author Comment

by:DreamingEagle
ID: 2735508
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
 
LVL 1

Author Comment

by:DreamingEagle
ID: 2735518
Sorry, Tim, I forgot to add the exta ten points like I promised I would.  I'll catch you next time, though.
0
 
LVL 1

Author Comment

by:DreamingEagle
ID: 2735750
Sorry, Tim, I forgot to add the exta ten points like I promised I would.  I'll catch you next time, though.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now