Scan SubFolders within SubFolders (1 Viewer)

aqif

Registered User.
Local time
Today, 06:54
Joined
Jul 9, 2001
Messages
158
Hi

I am trying to develope a utility which will scan all the subfolders within a folder and will return the total file count and file sizes in a tabular form. The code so far is only working up to first level of subfolders. What can I do in the code to make it work for all the indefinite levels of subfolders. I am sure it requires a little tweaking but not sure how.

Here's my Code
---------------

Dim objFSO As FileSystemObject
Dim objFolder As Folder
Dim objFiles As File
Dim strCount As Long
Dim strFileNames As String
Dim strSize As Long
Dim strTotalCount, strTotalSize As Long

Me.TxtStatistics = Null
Me.TxtFileAtt = Null

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("D:\Aqif")

'To scan root files
'------------------

For Each objFiles In objFolder.Files


strSize = strSize + Round(objFiles.Size / 1024, 2)
strCount = strCount + 1
Next
Me.TxtStatistics = Me.TxtStatistics & vbCrLf & " " & objFolder.Path '& vbCrLf & "Total files: " & strCount & " " & "Total size (in KB): " & strSize & " (in MB): " & Round(strSize / 1024, 2) & vbCrLf
Me.TxtFileAtt = Me.TxtFileAtt & vbCrLf & " " & Right(" " & Format(strCount, "0"), 4) & " " & Right(" " & Format(strSize, "0.00"), 9) & " " & Right(" " & Format(strSize / 1024, "0.00"), 7)
strTotalCount = strCount
strTotalSize = strSize
strSize = 0
strCount = 0

'To scan sub folders
'-------------------
For Each objFolder In objFolder.SubFolders


For Each objFiles In objFolder.Files

strSize = strSize + Round(objFiles.Size / 1024, 2)
strCount = strCount + 1

Next
Me.TxtStatistics = Me.TxtStatistics & vbCrLf & " " & objFolder.Path '& vbCrLf & "Total files: " & strCount & " " & "Total size (in KB): " & strSize & " (in MB): " & Round(strSize / 1024, 2) & vbCrLf
Me.TxtFileAtt = Me.TxtFileAtt & vbCrLf & " " & Right(" " & Format(strCount, "0"), 4) & " " & Right(" " & Format(strSize, "0.00"), 9) & " " & Right(" " & Format(strSize / 1024, "0.00"), 7)
strTotalCount = strTotalCount + strCount
strTotalSize = strTotalSize + strSize
strSize = 0
strCount = 0

Next

Msgbox (strTotalCount & "---" & strTotalSize)


Any suggestions?


Cheers!
Aqif
 

ecniv

Access Developer
Local time
Today, 06:54
Joined
Aug 16, 2004
Messages
229
You are going to want a recursive loop.
How you do it is up to you and I advise that you do it rather than getting code and its hard enough to try and debug, but if you haven't wrote it, it can be worse :/

I do have code that does exactly what you want except the file size/attributes etc, but I'm sure you could tweak that. But first, you have a go and post up if you get really stuck.

You'll need to pull the list in the dir and for each folder (type 16) pull alist again calling the same sub. Probably arrays and pointers (use static I think it was or persistant or something like that to ensure its new for each call but holds it for the previous call(s)).


Vince
 

Users who are viewing this thread

Top Bottom