Folder size in VB.NET?

T

TyBreaker

I have a vbscript which returns the folder size of any folder instantly
by referring to Folder.Size. I don't need any iteration or recursion
through the subfolders, adding together individual file sizes etc.

But I can't find anything comparable in VB.NET or am I missing
something? All the examples I have found online are iterative/recursive
solutions but these take a long time to complete if there are lots of
files/folders in a hierarchy. Windows obviously stores this data
somewhere handy for vbscript so surely there is a way to access this in
VB.NET too?
--
______ ___ __
/_ __/_ __/ _ )_______ ___ _/ /_____ ____
/ / / // / _ / __/ -_) _ `/ '_/ -_) __/
/_/ \_, /____/_/ \__/\_,_/_/\_\\__/_/
/___/

There are 10 types of people in this world; those who understand the
binary numbering system and those who don't.

There's no place like 127.0.0.1.

ASCII a silly question, get a silly ANSI.
 
L

Larry Lard

TyBreaker said:
I have a vbscript which returns the folder size of any folder instantly
by referring to Folder.Size. I don't need any iteration or recursion
through the subfolders, adding together individual file sizes etc.

But I can't find anything comparable in VB.NET or am I missing
something? All the examples I have found online are iterative/recursive
solutions but these take a long time to complete if there are lots of
files/folders in a hierarchy. Windows obviously stores this data
somewhere handy for vbscript so surely there is a way to access this in
VB.NET too?

I don't think Windows does store this info anywhere. For example, in
Windows Explorer, if you get the properties of a folder with many
subfolders, you can _see_ the size being recursively calculated. If it
were stored anywhere, surely Windows Explorer would be a prime
candidate for having access to that information.

When you invoke Folder.Size in VBScript you are using a method in
scrrun.dll, which (I suspect) runs the same recursive process you see
in the .NET sample for Directory. It's just that because scrrun exposes
it as a single method, you don't see the code. Just code up the
standard example, hide it away in a utility class, and you will forget
it is any more than a simple method call :)

It might be interesting to see if you can demonstrate a performance
difference between scrrun.dll's implementation, and a .NET
implementation.
 
S

Stephany Young

What makes you think that 'Windows obviously stores this data somewhere
handy for vbscript'?

If you ever used the FSO Folder.Size property in VBScript to get the size of
a folder that contains a large number of files you would know that the
response is by no means instantaneous. This implies that the property
actually calculated the value on the fly.

Now the question becomes, what do you interpret Folder.Size to mean? Do you
interpret it as the total number of bytes taken from the actual size of all
the files and subfolders in the folder or do you interpret as the number of
bytes representing the number of sectors taken up by all the files and
subfolders in the folder?

I note that you state that you don't need to 'recurse' thae subfolders but
that is what the FSO Folder.Size property does.

Given that methods and properties of the FSO were so damn slow it was
probably prudent to include an optimized property to give an approximate
value. (I call it approxomate because I never really figured out just what
it was telling me.)

Using the framework Directory and File classes to 'walk' the contents of a
directory is very fast and you are able to tailor it to give you the exact
information you want. (And you don't have to 'recurse' the sub-directories
if you don't want to.)
 
T

TyBreaker

Stephany said:
If you ever used the FSO Folder.Size property in VBScript to get the size of
a folder that contains a large number of files you would know that the
response is by no means instantaneous. This implies that the property
actually calculated the value on the fly.

Apologies, I re-ran my vbscript just now and it is slow too. Suffering
from selective memory disfunction I think. It does appear to be faster
on subsequent runs for some reason but yes, I admit defeat and will
crawl back into my cave :)

--
______ ___ __
/_ __/_ __/ _ )_______ ___ _/ /_____ ____
/ / / // / _ / __/ -_) _ `/ '_/ -_) __/
/_/ \_, /____/_/ \__/\_,_/_/\_\\__/_/
/___/

There are 10 types of people in this world; those who understand the
binary numbering system and those who don't.

There's no place like 127.0.0.1.

ASCII a silly question, get a silly ANSI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VB.NET Appbar example? 1
System Icons? 2
Prompting for an IP address 4
Form Layout 3
Dynamic buttons 2
Floating Menu Bar 6
VB2005: Controls not appearing on form 3
System Volume Information 3

Top