Human readable file sizes

  • Thread starter Jesús Gabriel y Galán
  • Start date
J

Jesús Gabriel y Galán

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

Jesús Gabriel y Galán

-----Original Message-----
From: Jes=FAs Gabriel y Gal=E1n [mailto:[email protected]]
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 =3D regexp.match(File.basename(file));
if match
size =3D 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.
 
S

Simon Kröger

Jesús Gabriel y Galán said:
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
 
W

William James

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

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top