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!

show free hard drive space error overflow

Status
Not open for further replies.

dtrinowitz

Programmer
Jan 29, 2005
12
DE
this programm is supposed to tell you the amount of free hard drive space by clicking on a button. The error im getting is the runtime6 overflow error.

Any suggestions?

Private Sub cmdSpace_Click()
Dim info As DiskInformation
Dim lAnswer As Long
Dim lpRootPathName As String
Dim lpSectorsPerCluster As Long
Dim lpBytesPerSector As Long
Dim lpNumberOfFreeClusters As Long
Dim lpTotalNumberOfClusters As Long
Dim lBytesPerCluster As Long
Dim lNumFreeBytes As Double
Dim sString As String

lpRootPathName = "c:\"
lAnswer = GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters)
lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector
this is the error line-> lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters
sString = sString & "Number of Free Megabytes: " & Format(((lNumFreeBytes / 1024) / 1024), "0.00") & "MB"
lstMain.Clear
lstMain.AddItem sString

End Sub

 
this is the module code which belongs to the above:

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

Public Type DiskInformation
lpSectorsPerCluster As Long
lpBytesPerSector As Long
lpNumberOfFreeClusters As Long
lpTotalNumberOfClusters As Long
End Type
 
Your answer for lNumFreeBytes will be too big for a Long data type if you have a modern-size hard drive. Move one of your integer divisions to an earlier stage.
Code:
lBytesPerCluster = lpSectorsPerCluster * lpBytesPerSector / 1024
'this is the error line-> 
lNumFreeBytes = lBytesPerCluster * lpNumberOfFreeClusters
sString = sString & "Number of Free Megabytes: " & Format(((lNumFreeBytes / 1024) / 1024), "0.00") & "MB"

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

For tsunami relief donations

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Alternatively, you could always consider using the filesystemobject
 
Or another API approach ...

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

regards Hugh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top