Public Declare Function GetDiskFreeSpaceEx Lib "kernel32" (ByVal lpDirectoryName As String, lpFreeBytesAvailableToCaller As Currency, lpTotalNumberOfBytes As Currency, lpTotalNumberOfFreeBytes As Currency) As Long
'nl for Win 95 pre OSR2
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
Function DiskSpace(Drive$) As Currency
Dim lpFreeBytesAvailableToCaller As Currency, lpTotalNumberOfBytes As Currency, lpTotalNumberOfFreeBytes As Currency
Dim lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long
Dim i As Currency
i = 1
Drive$ = Left$(Drive$, 2) & "\"
On Error Resume Next
If GetDiskFreeSpaceEx(Drive$, lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes) Then
If Err Then
'Win 95 prior to OSR2 dos'nt support GetDiskFreeSpaceEx
' note the function always returns 2048 MBs under Win 95 if space is greater than 2048 MBs
If GetDiskFreeSpace(Drive$, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters) Then
DiskSpace = i * lpSectorsPerCluster * lpBytesPerSector * lpNumberOfFreeClusters
End If
Else
DiskSpace = lpFreeBytesAvailableToCaller * (10 ^ 4)
End If
End If
Public Function DiskSpace(SourceDrive As String) As Currency
DiskSpace = CreateObject("scripting.filesystemobject").GetDrive(SourceDrive).AvailableSpace
End Function
It might be useful to format the results (free or total space) of GetDiskFreeSpaceEx function for reporting purposes.
Windows has two functions, StrFormatByteSize and StrFormatByteSize64 for converting file sizes to string format. StrFormatByteSize supports 32-bit sizes going up to 4 GB. See example in thread222-1166618.
For larger sizes, as most drives now have these days, you can use StrFormatByteSize64 which supports 64-bit sizes going up to 8 EB (2[sup]63[/sup] bytes).
See the following code.
___
[tt]
Option Explicit
Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, lpFreeBytesAvailableToCaller As Any, lpTotalNumberOfBytes As Any, lpTotalNumberOfFreeBytes As Any) As Long
Private Declare Function StrFormatByteSize64 Lib "shlwapi" Alias "StrFormatByteSize64A" (ByVal qdw As Currency, ByVal szBuf As String, ByVal uiBufSize As Long) As Long
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
Private Type LONGLONG
qdw As Currency
End Type
Private Sub Form_Load()
Dim liTotal As LARGE_INTEGER, liFree As LARGE_INTEGER
GetDiskFreeSpaceEx "C:\", ByVal 0&, liTotal, liFree
Debug.Print "Total Space = " & FormatByteSize(liTotal)
Debug.Print "Free Space = " & FormatByteSize(liFree)
Unload Me
End Sub
Private Function FormatByteSize(li As LARGE_INTEGER) As String
Dim ll As LONGLONG, S As String * 10
LSet ll = li
StrFormatByteSize64 ll.qdw, S, Len(S)
FormatByteSize = Left$(S, InStr(S, vbNullChar) - 1)
End Function[/tt]
___
Also, if you use strongm's FSO method, then you can use strongm's human_file_size function in above-mentioned thread to format the byte size.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.