Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check harddisk space

Status
Not open for further replies.

jack1080

Programmer
Jun 2, 2007
34
MY
Hi, i wish to check the user C drive remaining spaces, how to do this?
 
Try;

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

End Function
 
Or even:

Public Function DiskSpace(SourceDrive As String) As Currency
DiskSpace = CreateObject("scripting.filesystemobject").GetDrive(SourceDrive).AvailableSpace
End Function
 
Strongm,

That looks very good (and short). A couple of questions;

What versions of Windows (or IE) is it good from.

Is it possible (for say a paranoid network admin)to disable the scripting.filesystemobject.

Oh and er.. Just when is it going to stop raining?

regards Hugh
 
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.
 
>Just when is it going to stop raining?

I don't think even strongm knows the answer to that one!

He may need to spend the next 40 days and 40 nights working it out.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top