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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

not adding up

Status
Not open for further replies.

warren2600

Programmer
Jun 28, 2004
13
0
0
US
need another pair of eyes to check out my code below.
i have several check and text boxes which calculate the size of a folder.
All of loops work, except one.
All of the if statements are the same.
i've added another variable in there just for the hell of it, and it calculates it correctly.
what am i missing?

thanks in advance for any input

warren



Private Function foldersize(sFolder As String, ndestination As Object)

Dim FindInfo As WIN32_FIND_DATA
Dim lFindHandle As Long
Dim lTemp As Long
Dim dSize As Double
Dim bNextLoop As Boolean
Dim sFileName As String
Dim fsize As Double



'Open up an API FIND on the folder.
lFindHandle = FindFirstFile(sFolder + "\*", FindInfo)
If lFindHandle <> INVALID_HANDLE_VALUE Then

Do
sFileName = FindInfo.cFileName
sFileName = Replace(sFileName, Chr(0), " ")
sFileName = Trim(sFileName)

If sFileName <> "." And sFileName <> ".." Then

If (FindInfo.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
'We're on a folder.
dSize = dSize + foldersize(sFolder + "\" + sFileName, ndestination)
Else
'We're on a file.
dSize = dSize + FindInfo.nFileSizeLow
dSize = dSize + (FindInfo.nFileSizeHigh * (2 ^ 16))
End If

End If

FindInfo.cFileName = String(255, vbNullChar)
lTemp = FindNextFile(lFindHandle, FindInfo)
bNextLoop = (lTemp <> ERROR_NO_MORE_FILES) And (lTemp <> 0)

Loop While bNextLoop

'Close down the file handle
FindClose lFindHandle
End If


fsize = fsize + dSize '--------> fsize isn't adding up

file_total = file_total + fsize '----------->another variable which i assign to another text box which works



Call returnsize(fsize, ndestination) 'call to next function to assign proper filesuffix

End Function



'----------------------------------------------'
' append suffix '
'----------------------------------------------'

Private Function returnsize(sSize As Double, ByVal ndestination As Object)
Dim filesuffix As String

If sSize > 1024 Then
sSize = sSize / 1024
filesuffix = " KB"
If sSize > 1024 Then
sSize = sSize / 1024
filesuffix = " MB"
If sSize > 1024 Then
sSize = sSize / 1024
filesuffix = " GB"
End If
End If
End If

ndestination = Format(sSize, "###0.00") & filesuffix
End Function
 
fsize is reinitialized every time you call the function foldersize. I'm not sure what you mean by doesn't add up- it should be equal to dsize each time you call the function. In fact it is not really necessary to have the variable fsize- just send dsize to the returnsize function.

If you are trying to add up fsize outside of multiple calls to the foldersize function you need to declare it outside the function or increment it in whatever code calls the foldersize function.

Or do you want fsize incremented inside the loop? It's hard to surmise your desired output from the description.
 
Also -
Are you looking for the size of the items in the folder as they would occupy in memory, or are you looking for the space they occupy on disk?

The difference comes about because disk systems use an allocation unit (for example, NTFS by default is 4k). So even if you have a 1 byte file, the smallest amount of disk space it can take up is 4096 bytes.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Heh heh heh - I recognise that code... Someone's read my FAQ!

I'm a bit puzzled on that Returnsize function. My first question being "Why pass an object by value" - as the Initialise event etc would fire...

The second question, on closed inspection, is why pass the object at all - it's not being used.

mmilan.
 
Why not use the FSO?
___
[tt]
Private Sub Form_Load()
MsgBox GetFolderSize("C:\Windows")
End
End Sub

Public Function GetFolderSize(Folder As String) As Long
GetFolderSize = CreateObject("Scripting.FileSystemObject").GetFolder(Folder).Size
End Function[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top