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,577 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

9 Experts available now in Live!

Get 1:1 Help Now