Getting accurate folder details

I have a script to read in a list of folders and for each one processed, try and retrieve the size of the folder, as well as number of files/folders contained therein (the exact results in a right-click property window on the folder would be ideal).

However, it seems using fso, I can't get accurate results.  One folder I'm testing against shows nearly 30mb with nearly 2000 files contained in over 300 folders.  My report shows the size (not the 'size on disk' - would love to have both), but only shows 3 folders and 0 for files...

Is there a reliable method to pull these details?
LVL 67
sirbountyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
Hi, SB.

Are you looping through all of the folders to get eh information or jsut reading the top level folder?
0
David LeeCommented:
Geez, pardon all the typos.  My fingers and brain don't always talk to each other.
0
sirbountyAuthor Commented:
Np - I've just been reading the top level.  I thought it 'had' worked in the past...now I'm not so sure... : (
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

David LeeCommented:
I don't think there's a way to do this without iterating through the folders.  Even Explorer does that.  It does it a lot faster, but it still does it.
0
sirbountyAuthor Commented:
Hmm - so I basically need some sort of counter for the files/folders then...  I think the size is actually correct for the entire folder though...
0
David LeeCommented:
Something like this will do it.
Dim objFSO, varFiles, varFolders, varSize
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
varFolders = -1
ProcessFolder objFSO.GetFolder("C:\SomeFolder")
Set objFSO = Nothing
'Code for doing something with the results goes here'
WScript.Quit
 
Sub ProcessFolder(objFolder)
    Dim objFile, objSubFolder
    For Each objFile In objFolder.Files
        varFiles = varFiles + 1
        varSize = varSize + objFile.Size
    Next
    varFolders = varFolders + 1
    For Each objSubFolder In objFolder.SubFolders
        ProcessFolder objSubFolder
    Next
    Set objFile = Nothing
    Set objSubFolder = Nothing
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alan_WhiteCommented:
Yeah, the folder size works at top level (and very efficiently too) but if you want file count you have to trawl though each one.
0
sirbountyAuthor Commented:
BDF - I'm off today, so I'll check this out on Monday.
Doesn't seem like I need to iterate the contents for size tho - I think that works fine as-is...this will still save me a bit of coding though - thanks.
0
David LeeCommented:
No problem.  

If there's no need to total up the size, then we can modify the code and skip iterating the files.  That should speed this up a little.
Dim objFSO, varFiles, varFolders
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
varFolders = -1
ProcessFolder objFSO.GetFolder("C:\Users\David\Documents\Excel")
Set objFSO = Nothing
 
Sub ProcessFolder(objFolder)
    Dim objSubFolder
    varFiles = varFiles + objFolder.Files.count
    varFolders = varFolders + 1
    For Each objSubFolder In objFolder.SubFolders
        ProcessFolder objSubFolder
    Next
    Set objFile = Nothing
    Set objSubFolder = Nothing
End Sub

Open in new window

0
sirbountyAuthor Commented:
Sorry for the delay - running into some permission problems.
It's of course something that shouldn't be a problem, but for whatever reason, it occasionally is.
I hate to simply ignore those folders, so I'm presently working on something to find and correct the problem before processing the folder.
Whether I can or cannot get this working though, I'll close this out shortly, because it is giving me what I need on the 'working' folders.  Thanks!
0
sirbountyAuthor Commented:
Thanks for the help!
0
David LeeCommented:
No problem.  You're welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.