Solved

VB.Net - A fast way to find folder size or number of files in a folder/sub folders

Posted on 2013-01-01
2
2,619 Views
Last Modified: 2013-01-02
Hi Experts.  I am writting a program that iterates through very large folders (100G to 500G) and I need to be able to show the progress of the program.   To do that I first need a fast way to get either the size of the folder or the number of files in the folder.   So this way I guage the progress and show it.      

A short wait while the process prepares itself would be OK for the user, but iterating through the folder is impractical as the program does this anyway and it takes too long.    

I see scripting has a way to do it quickly but I understand this is frowned upon in VB.Net.

It must of course include all sub folders

As Windows seems to have this data available anyway, perhaps there is a way to access this quickly?

Many thanks
0
Comment
Question by:PNRT
2 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 250 total points
ID: 38734596
Unfortunately .NET doesn't have a good way to calculate directory sizes. Windows does this the same way you are doing it, by iterating through all the files. You can try using Directory.GetFiles with the SearchOption.AllDirectories but that might be a bit flaky.

I think your best bet might be to spawn a worker thread and do the calculation there. That way you can update the UI when the calculation is done without making the user wait for it.
0
 
LVL 9

Assisted Solution

by:sognoct
sognoct earned 250 total points
ID: 38734763
from this article :
http://www.codeproject.com/Articles/38959/A-Faster-Directory-Enumerator

And as  Russ_Suter correctly says also filesystem takes the exact time of
 Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length

So count files and add a counter in the worker, give progressbar1.maximumvalue the number of files and assign progressbar1.value the current counter value.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now