Getting Folder Size

C

Clement Ow

When I use File.size("C:/ruby"), all it returns is 0.
But for files it does work fine. So is there a way to calculate folder
size?
 
T

Torsten Mangner

Clement said:
When I use File.size("C:/ruby"), all it returns is 0.
But for files it does work fine. So is there a way to calculate folder
size?

since a directory is just something virtual, it has a size of 0.

you will have to get all of the files the folder includes (recursive)
and sum their sizes up.
 
T

Torsten Mangner

Torsten said:
since a directory is just something virtual, it has a size of 0.

you will have to get all of the files the folder includes (recursive)
and sum their sizes up.

or use your operating system to determine the folder size ... like

du -s dir

under unix systems.
 
C

Clement Ow

Torsten said:
or use your operating system to determine the folder size ... like

du -s dir

under unix systems.

I have options that has nonrecursive requirements.. There isnt a direct
method at all that calculates DIR size? Hmmmm, I'm using Windows XP Pro,
so dont suppose the Unix command would work. But, If i use Windows
command, would it probable then?

Cheers
 
G

Gordon Thiesfeld

I have options that has nonrecursive requirements.. There isnt a direct
method at all that calculates DIR size? Hmmmm, I'm using Windows XP Pro,
so dont suppose the Unix command would work. But, If i use Windows
command, would it probable then?
 
S

Seebs

I have options that has nonrecursive requirements.. There isnt a direct
method at all that calculates DIR size? Hmmmm, I'm using Windows XP Pro,
so dont suppose the Unix command would work. But, If i use Windows
command, would it probable then?

There is no non-recursive solution. There are solutions that put the
recursion in another application, but that's it.
 
J

John Joyce

There is no non-recursive solution. There are solutions that put the
recursion in another application, but that's it.
To explain it a little further... technically, even the system is
doing some recursion here when it shows you a folder size.
Directories don't really have any size to speak of since they are
really just abstract ways to organize data. Doing the recursion is a
good idea. It gives you a chance to get a more accurate result.
Sometimes the system call may be faster though depending on just how
many files are *in* a directory. The system may have some meta data
available already.
 
D

Dave Bass

John said:
To explain it a little further... technically, even the system is
doing some recursion here when it shows you a folder size.

If you right-click on a large Windows folder such as My Documents and
select Properties (in XP at any rate) you can see the system recursing
through and adding up the number of bytes. In theory this figure could
be made available instantly if the system kept track of what was going
on in each folder. But presumably that would slow things down (more).
 
P

phlip

Dave said:
John Joyce wrote:

If you right-click on a large Windows folder such as My Documents and
select Properties (in XP at any rate) you can see the system recursing
through and adding up the number of bytes. In theory this figure could
be made available instantly if the system kept track of what was going
on in each folder. But presumably that would slow things down (more).

Right. In terms of transactions, a file system is a database tuned to rapidly
read and write individual files. Stashing their intermediate sizes - into every
directory entry in a system - would slow down every single write to every file,
and nobody wants that.

Could the original poster attempt what they really need to do, in some other
way? A spot-check of a limited number of file sizes should be relatively cheap...
 
C

Clement Ow

phlip said:
Right. In terms of transactions, a file system is a database tuned to
rapidly
read and write individual files. Stashing their intermediate sizes -
into every
directory entry in a system - would slow down every single write to
every file,
and nobody wants that.

Could the original poster attempt what they really need to do, in some
other
way? A spot-check of a limited number of file sizes should be relatively
cheap...

Thanks for all your input, guys, really helped me alot! I actually went
along with the WIN32OLE method, which is totally sweet especially when
you have so many options and lines of code is quite hard to implement a
recursive just calculate the file size(i also dont want to compromise on
the efficiency of the script) =) Thanks once again! Really nice workin
with you guys! =D
 
C

Clement Ow

From: (e-mail address removed)
# Thanks for all your input, guys, really helped me alot! I
# actually went
# along with the WIN32OLE method, which is totally sweet
# especially when
# you have so many options and lines of code is quite hard to
# implement a
# recursive just calculate the file size(i also dont want to
# compromise on
# the efficiency of the script) =) Thanks once again! Really
# nice workin
# with you guys! =D

interesting. imho, i find pure ruby's Find.find faster, more expressive,
shorter, and portable, eg,


#=> nil

#=> 24569727


Hmm yea, this is quite an interesting way too, and more handy if you
need to have more options like finding the size or dir count of the
files in a certain directory. It is also easier to modify the code and
get what you want eh? Thanks for the input botp! =) It has been a great
learning experience so far!

But just curious actually, that if there is any documentation that
pertains to the win32ole methods, not the one from the ruby
documentation of WIN32OLE but more of the methods that come along with
it, not really sure how to explain but examples like these:

folder = fso.GetFolder('C:\ruby\scripts')
=> "C:\\ruby\\scripts"

so far I only know of GetFolder,GetFile, name, size, path kinda methods.
just wondering if anyone knows if there is a list of such methods which
might be really helpful in manipulating files and folders?

Also, for fso = WIN32OLE.new('Scripting.FileSystemObject')is
FileSystemObject part of ruby? The documentation in ruby states that:
"The first argument should be CLSID or PROGID. If second argument host
specified, then returns OLE Automation object on host." Does anyone know
what this means?

Thanks!
 
P

Peña, Botp

RnJvbTogY2xlbWVudC5vd0Bhc2lhLmJucHBhcmliYXMuY29tIA0KIyBCdXQganVzdCBjdXJpb3Vz
IGFjdHVhbGx5LCB0aGF0IGlmIHRoZXJlIGlzIGFueSBkb2N1bWVudGF0aW9uIHRoYXQgDQojIHBl
cnRhaW5zIHRvIHRoZSB3aW4zMm9sZSBtZXRob2RzLCBub3QgdGhlIG9uZSBmcm9tIHRoZSBydWJ5
IA0KDQpjbGVtZW50LCB3aW4zMm9sZSBpcyBtYWlubHkgbWljcm9zb2Z0LWlzbS4gUnVieSdzIHdp
bjMyb2xlIGlzIGp1c3QgYSBiZWF1dGlmdWwgcnVieSB3cmFwcGVyIGZvciB3aW5kb3dzIG9sZS4g
WW91J2xsIGhhdmUgdG8gZGlnIHRoZSBtaWNyb3NvZnQgZHNuIHNpdGUgb3IgZG93bmxvYWQgc29t
ZSBmcmVlIG9sZSBicm93c2VycyB0byBnZXQgYSBmdWxsIGxpc3RpbmcvZG9jdW1lbnRhdGlvbiBv
ZiB0aGUgb2xlIG1ldGhvZHMgaW4gd2luZG93cy4uLiBZb3UgY2FuIGNyZWF0ZSB5b3VyIG93biBv
bGUgZGxsIGJ1dCB0aGVuIGFnYWluLCB5b3Ugc3RpbGwgaGF2ZSB0byByZWFkIG1pY3Jvc29mdCBz
cGVjcyBvbiB0aGF0IHRvby4uLg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 
C

Clement Ow

From: (e-mail address removed)
# But just curious actually, that if there is any documentation that
# pertains to the win32ole methods, not the one from the ruby

clement, win32ole is mainly microsoft-ism. Ruby's win32ole is just a
beautiful ruby wrapper for windows ole. You'll have to dig the microsoft
dsn site or download some free ole browsers to get a full
listing/documentation of the ole methods in windows... You can create
your own ole dll but then again, you still have to read microsoft specs
on that too...

kind regards -botp

Ah icic. Very interesting indeed.. :)So to know what to put in the
string after new, WIN32OLE.new('Scripting.FileSystemObject')I have to go
to the microsoft dsn site to see what is in the syntax? Would it be a
universal type of syntax here that can be used with any other types of
programming languages that support WIN32OLE?
 

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

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top