Human readable file sizes

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.

I think this is what you want

GIGA_SIZE = 1073741824.0
MEGA_SIZE = 1048576.0
KILO_SIZE = 1024.0

Return the file size with a readable style.

def readable_file_size(size, precision)
case
when size == 1 : “1 Byte”
when size < KILO_SIZE : “%d Bytes” % size
when size < MEGA_SIZE : “%.#{precision}f KB” % (size / KILO_SIZE)
when size < GIGA_SIZE : “%.#{precision}f MB” % (size / MEGA_SIZE)
else “%.#{precision}f GB” % (size / GIGA_SIZE)
end
end

something like what df -h does: make an approximation to the nearest

Thanks,

Jesus.

ls -Qsh1

HTH,

Felix

Jesús Gabriel y Galán schrieb:

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

On 10/4/07, Felix W. [email protected] wrote:

-----Original Message-----
From: Jesús Gabriel y Galán [mailto:[email protected]]

    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.

On Oct 4, 5:05 am, “Jesús Gabriel y Galán” [email protected]
wrote:

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 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.html#M000525

On 10/4/07, Daniel W. [email protected] 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.html#M000525

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

Thanks again,

Jesus.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs