[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting Folder Size

Clement Ow

7/4/2008 6:20:00 AM

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?
--
Posted via http://www.ruby-....

13 Answers

Torsten Mangner

7/4/2008 6:48:00 AM

0

Clement Ow wrote:
> 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.
--
Posted via http://www.ruby-....

Torsten Mangner

7/4/2008 6:53:00 AM

0

Torsten Mangner wrote:

> 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.
--
Posted via http://www.ruby-....

Clement Ow

7/4/2008 7:07:00 AM

0

Torsten Mangner wrote:
> Torsten Mangner wrote:
>
>> 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.

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
--
Posted via http://www.ruby-....

Gordon Thiesfeld

7/5/2008 3:57:00 AM

0

On Fri, Jul 4, 2008 at 2:06 AM, Clement Ow
<clement.ow@asia.bnpparibas.com> wrote:
> 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?
>


>> require 'win32ole'
=> true
>> fso = WIN32OLE.new('Scripting.FileSystemObject')
=> #<WIN32OLE:0x2c94060>
>> folder = fso.GetFolder('C:\ruby\scripts')
=> #<WIN32OLE:0x2c90a48>
>> folder.name
=> "scripts"
>> folder.size
=> 226746920
>> folder.path
=> "C:\\ruby\\scripts"
>>

Seebs

7/5/2008 5:58:00 PM

0

On 2008-07-04, Clement Ow <clement.ow@asia.bnpparibas.com> wrote:
> 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.

--
Copyright 2008, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!

John Joyce

7/5/2008 8:15:00 PM

0


On Jul 5, 2008, at 1:16 PM, Seebs wrote:

> On 2008-07-04, Clement Ow <clement.ow@asia.bnpparibas.com> wrote:
>> 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.
>
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.


Dave Bass

7/6/2008 1:28:00 PM

0

John Joyce wrote:
> 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).
--
Posted via http://www.ruby-....

Phlip

7/6/2008 1:42:00 PM

0

Dave Bass wrote:

> John Joyce wrote:

>> 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).

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...

Clement Ow

7/7/2008 7:24:00 AM

0

phlip wrote:
> Dave Bass wrote:
>
>> John Joyce wrote:
>
>>> 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).
>
> 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
--
Posted via http://www.ruby-....

Clement Ow

7/7/2008 8:47:00 AM

0

Peña, Botp wrote:
> From: clement.ow@asia.bnpparibas.com
> # 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,
>
>>dirsize =0
>
>>Find.find("c:/ruby187") do |f| dirsize += File.stat(f).size end
> #=> nil
>
>>dirsize
> #=> 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')
=> #<WIN32OLE:0x2c90a48>
>> folder.name
=> "scripts"
>> folder.size
=> 226746920
>> folder.path
=> "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!

--
Posted via http://www.ruby-....