p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access VBA (http://p2p.wrox.com/forumdisplay.php?f=80)
-   -   Free space on disc (http://p2p.wrox.com/showthread.php?t=39026)

elansolutionsltd February 25th, 2006 01:17 PM

Free space on disc
Due to some strange managerial decisions, I run a process on a 200Gb USB drive. However, the process eats a lot of space as it is used to unzip some huge text files before processing the files into Access. Is there a way that I can interrogate the drive (or any other drive, for that matter) from the process' main form to discover how much free space exists on it before I start the process, rather than the system stopping when the disc is full?


Bob Bedell February 25th, 2006 07:26 PM


The GetDiskFreeSpaceEx Win32 API call should get it for you. Just change the letter of your disk drive in the Test routine (my external HD is drive E:). I'm not sure what the difference is between the Free and Availble values reported. They are identical on my system.


Option Compare Database
Option Explicit

    lowpart As Long
    highpart As Long
End Type

Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias _
    "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, _
    lpFreeBytesAvailableToCaller As LARGE_INTEGER, lpTotalNumberOfBytes _
    As LARGE_INTEGER, lpTotalNumberOfFreeBytes As LARGE_INTEGER) As Long

Sub Test()
    Dim lResult As Long
    Dim liAvailable As LARGE_INTEGER
    Dim liTotal As LARGE_INTEGER
    Dim liFree As LARGE_INTEGER
    Dim dblAvailable As Double
    Dim dblTotal As Double
    Dim dblFree As Double

    'Determine the Available Space, Total Size and Free Space of a drive
    lResult = GetDiskFreeSpaceEx("E:\", liAvailable, liTotal, liFree)

    'Convert the return values from LARGE_INTEGER to doubles
    dblAvailable = CLargeInt(liAvailable.lowpart, liAvailable.highpart)
    dblTotal = CLargeInt(liTotal.lowpart, liTotal.highpart)
    dblFree = CLargeInt(liFree.lowpart, liFree.highpart)

    'Display the results
    Debug.Print "Available Space:  " & dblAvailable & " bytes (" & _
                Format(dblAvailable / 1024 ^ 3, "0.00") & " G) " & vbCr & _
                "Total Space:      " & dblTotal & " bytes (" & _
                Format(dblTotal / 1024 ^ 3, "0.00") & " G) " & vbCr & _
                "Free Space:      " & dblFree & " bytes (" & _
                Format(dblFree / 1024 ^ 3, "0.00") & " G) "

End Sub

Private Function CLargeInt(Lo As Long, Hi As Long) As Double

    'This function converts the LARGE_INTEGER data type to a double

    Dim dblLo As Double, dblHi As Double

    If Lo < 0 Then
        dblLo = 2 ^ 32 + Lo
        dblLo = Lo
    End If

    If Hi < 0 Then
        dblHi = 2 ^ 32 + Hi
        dblHi = Hi
    End If

    CLargeInt = dblLo + dblHi * 2 ^ 32

End Function



Bob Bedell February 25th, 2006 09:27 PM

MSDN says:


A pointer to a variable that receives the total number of free bytes on a disk.


A pointer to a variable that receives the total number of free bytes on a disk that are available to the user who is associated with the calling thread.


elansolutionsltd February 26th, 2006 08:17 AM

Many thanks Bob, you've saved my phone ringing at 2 a.m. with an irate user telling me "your programme's fallen over". Now they will know they have to perform houskeeping before they run.


All times are GMT -4. The time now is 06:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.