On 10/4/07, Felix Windt <fwmailinglists / gmail.com> wrote:
> > -----Original Message-----
> > From: Jes Gabriel y Gal [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.