Re: Get total folder+subfolder size in VBA by Mark
Mark
Sun Feb 29 08:39:50 CST 2004
Perry,
OK, here's the FindFirst/Find Next, and following it is the 2-procedure
chain that calls it.
And for some reason, today (having not touched the code), instead of
giving the quick, if incorrect, result, it's running for several minutes
and (to judge from the value of certain variables when I hit Break)
iterating through all the folders on the hard drive.
Thanks again,
MT
Private Sub RecursiveFileSearch(FP As FILE_PARAMS)
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim sRoot As String
Dim spath As String
Dim fso As New FileSystemObject
Dim MyFolder As Folder
Dim sTmp As String
sRoot = QualifyPath(FP.sFileRoot)
spath = sRoot & FP.sFileNameExt
hFile = FindFirstFile(spath, WFD)
'if valid ...
If hFile <> INVALID_HANDLE_VALUE Then
Do
If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) _
And Asc(WFD.cFileName) <> vbDot Then
sTmp = TrimNull(WFD.cFileName)
If (sTmp <> ".") And (sTmp <> "..") Then
FP.sFileRoot = sRoot & sTmp 'adjust root
FP.Count = FP.Count + 1 'new count
Set MyFolder = fso.GetFolder(sRoot & sTmp)
lFileNum = lFileNum + MyFolder.Files.Count
lSize = lSize + MyFolder.Size
lFolders = FP.Count
RecursiveFileSearch FP
End If
End If
Loop While FindNextFile(hFile, WFD)
hFile = FindClose(hFile)
End If
Set fso = Nothing
Exit Sub
Foutje:
Resume Next
End Sub
'============
Sub A_021747()
GetFolderData "C:\MyDir"
End Sub
'============
Sub GetFolderData(DirName)
Dim FP As FILE_PARAMS
'Point to local occurrence of error object and handle locally
On Local Error GoTo InitError
lSize = 0
lFileNum = 0
lFolders = 0
DirName = ""
With FP
.sFileRoot = DirName '<< ROOTFOLDER
.sFileNameExt = "*"
.bRecurse = True
End With
On Error GoTo 0
RecursiveFileSearch FP
MsgBox "Size of all files: " & lSize & vbCr & "Number of files: " _
& lFileNum & vbCr & "Number of folders: " & lFolders
ExitHere:
Exit Sub
InitError:
MsgBox "catch the error"
Resume ExitHere
End Sub
Perry wrote:
>>following. Note line 6, which reads simply Me.TextBox1. Was something
>>else supposed to be there?
>
>
> You're very true. Delete that line of code ... This line was a result of the
> copy
> paste action I had to perform to delete some redudant lines. Delete that
> line.
> No influence on the erraneous results y're getting.
>
> Can you kick in the FindFistFile/FindNextFile loop passage of the code?
> And the subroutine or code passage in which you transfer the file
> attributes?
>
> Don't have any problems here ... I've used this code in several projects
> and results are ok.
>
> Krgrds,
> Perry
>
> "Mark Tangard" <Mark@NoMailPlease_Tangard.com> schreef in bericht
> news:O33fwfU%23DHA.2644@TK2MSFTNGP11.phx.gbl...
>
>>Hmm, OK, got that in. But what's happening now is, no matter what
>>folder I ask it to process, it's spitting out extremely wrong answers.
>>They're so wrong, I can't even see a pattern. For example:
>>
>>For a folder and its 3 subfolders and 2 sub-subfolders, which combined
>>total 17MB in 188 files, the macro reports a total of 21MB in 28 files.
>>
>>For a folder and its one subfolder, all combined containing 259KB in 8
>>files, it reports 54KB and a total of *one* file. (The only clue here
>>is that 54KB is the size of the one file in the one subfolder.)
>>
>>I thought at first I might've bothced the mild editing I did to turn the
>>CommandButton1_Click code into an ordinary sub (I'm using this in a
>>regular macro, not in a userform); but I put back the unedited code and
>>the same result occurs.
>>
>>What could be at the root of this??
>>
>>One other thing: The code for the CommandButton1_Click begins with the
>>following. Note line 6, which reads simply Me.TextBox1. Was something
>>else supposed to be there?
>>
>>Dim FP As FILE_PARAMS, strSize As String
>>On Local Error GoTo InitError
>>lSize = 0
>>lFileNum = 0
>>lFolders = 0
>>Me.TextBox1
>>With FP
>> .sFileRoot = Me.TextBox1 '<< ROOTFOLDER
>> .sFileNameExt = "*"
>> .bRecurse = True
>>End With
>>RecursiveFileSearch FP
>> '<-----etc.
>>
>>Thanks again for helping out with this.
>>
>>--
>>Mark Tangard, Microsoft Word MVP
>>Note well: MVPs do not work for Microsoft.
>>"Life is nothing if you're not obsessed." --John Waters
>>
>
>
>