[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Human readable file sizes

Jesús Gabriel y Galán

10/4/2007 10:05:00 AM

Hi,

I have a list of files and I need to produce a string containing the
size of each file, but in human readable format. With that I mean
something like what df -h does: make an approximation to the nearest
unit (B, KB, MB, GB, etc). Is this already done somewhere? This is
what I have now:

Find.find(folder) do |file|
match = regexp.match(File.basename(file));
if match
size = File.stat(file).size
puts size # would like a human readable format
end
end

Thanks,

Jesus.

5 Answers

Jesús Gabriel y Galán

10/4/2007 12:52:00 PM

0

On 10/4/07, Felix Windt <fwmailinglists@gmail.com> wrote:
> > -----Original Message-----
> > From: Jesús Gabriel y Galán [mailto:jgabrielygalan@gmail.com]

> > I have a list of files and I need to produce a string containing the
> > size of each file, but in human readable format. With that I mean
> > something like what df -h does: make an approximation to the nearest
> > unit (B, KB, MB, GB, etc). Is this already done somewhere? This is
> > what I have now:
> >
> > Find.find(folder) do |file|
> > match = regexp.match(File.basename(file));
> > if match
> > size = File.stat(file).size
> > puts size # would like a human readable format
> > end
> > end
> >
> > Thanks,
> >
> > Jesus.
>
> ls -Qsh1

Sorry, the above requirements are a simplification. For one I need to
retrieve only the file names that match a regexp, then depending on
parameters I need to gzip or delete the files and send an email with
the report of everything that was done and the free space before and
after in the disk, and some more things. So I'm writing a ruby program
to do that. I have a loop similar to the one above that does more
things, but one of them is to retrieve the file size for reporting,
and I would like to show human readable format a la -h flag for ls,
df, etc.

If there's no solution already implemented in places like File,
FileUtils or so (I haven't found it) I'll go with a custom solution
similar to the solution posted by Huang Zhimin.

Thanks,

Jesus.

Simon Kröger

10/4/2007 2:33:00 PM

0

Jesús Gabriel y Galán schrieb:
> Hi,
>
> I have a list of files and I need to produce a string containing the
> size of each file, but in human readable format. With that I mean
> something like what df -h does: make an approximation to the nearest
> unit (B, KB, MB, GB, etc). Is this already done somewhere? This is
> what I have now:
>
> Find.find(folder) do |file|
> match = regexp.match(File.basename(file));
> if match
> size = File.stat(file).size
> puts size # would like a human readable format
> end
> end
>
> Thanks,
>
> Jesus.

Like this:

----------------------------------------------
class Numeric
def to_human
units = %w{B KB MB GB TB}
e = (Math.log(self)/Math.log(1024)).floor
s = "%.3f" % (to_f / 1024**e)
s.sub(/\.?0*$/, units[e])
end
end
----------------------------------------------

Note: this may be not 100% correct for very large values (like TB) because
Math.log uses floating point arithmetic.

cheers

Simon

Daniel Waite

10/4/2007 3:28:00 PM

0

Jesús Gabriel y Galán wrote:
> I have a list of files and I need to produce a string containing the
> size of each file, but in human readable format. With that I mean
> something like what df -h does: make an approximation to the nearest
> unit (B, KB, MB, GB, etc). Is this already done somewhere?

http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.ht...
--
Posted via http://www.ruby-....

William James

10/4/2007 3:56:00 PM

0

On Oct 4, 5:05 am, "Jes?s Gabriel y Gal?n" <jgabrielyga...@gmail.com>
wrote:
> Hi,
>
> I have a list of files and I need to produce a string containing the
> size of each file, but in human readable format. With that I mean
> something like what df -h does: make an approximation to the nearest
> unit (B, KB, MB, GB, etc). Is this already done somewhere? This is
> what I have now:
>
> Find.find(folder) do |file|
> match = regexp.match(File.basename(file));
> if match
> size = File.stat(file).size
> puts size # would like a human readable format
> end
> end
>
> Thanks,
>
> Jesus.

def kilo n
count = 0
while n >= 1024 and count < 4
n /= 1024.0
count += 1
end
format("%.2f",n) + %w(B KB MB GB TB)[count]
end

[1,1024,1_000_000,8_000_000,9_000_000_000,
3_000_000_000_000].each{|n|
puts kilo(n)
}

Jesús Gabriel y Galán

10/5/2007 6:25:00 AM

0

On 10/4/07, Daniel Waite <rabbitblue@gmail.com> wrote:
> Jesús Gabriel y Galán wrote:
> > I have a list of files and I need to produce a string containing the
> > size of each file, but in human readable format. With that I mean
> > something like what df -h does: make an approximation to the nearest
> > unit (B, KB, MB, GB, etc). Is this already done somewhere?
>
> http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.ht...

Thanks to everybody who answered. I think I'll go with the action view
helper, cause it's already done :-).

Thanks again,

Jesus.