On Aug 29, 2008, at 4:56 AM, Thibaut Barrère wrote:
File.size(name)
can anyone think of a shorter or more readable way of writing it ? Do
you know a library that would do this ?
cheers,
Thibaut Barrère / LoGeek
http://blog.logeek.fr - learning content for developers
http://evolvingworker.com - tools for a better day
here is an ancient ruby script i use for that, it also considers
gzip’d files uncompressed but you could comment that out:
cfp:~ > dirsum src/ruby
src/ruby:
b: 768067279.000
kb: 750065.702
mb: 732.486
cfp:~ > cat bin/dirsum
#! /usr/bin/env ruby
at_least_one = false
ARGV.each do |dir|
begin
sum = 0
Dir[“#{ dir }//”].each do |path|
if test ?f, path
p path if $DEBUG
begin
case path
when /.(?:gz|z)\s*$/io
cmd = “{ gzip -l #{ path } | tail -1 | awk ‘{print
$2}’ ;} 2>/dev/null”
out = #{ cmd }
raise unless $?.exitstatus == 0
sum += out.to_i
else
sum += File.stat(path).size
end
rescue
warn “failed to sum <#{ path }>”
end
end
end
units = [
[:b , (2 ** 0)],
[:kb , (2 ** 10)],
[:mb , (2 ** 20)],
[:gb , (2 ** 30)],
[:tb , (2 ** 40)],
[:pb , (2 ** 50)],
]
puts "#{ dir }:"
units.each do |(label, size)|
q = sum / size.to_f
printf " %s: %.3f\n", label, q if q >= 1
end
at_least_one = true
rescue => e
warn “failed to sum <#{ dir }>”
next
end
end
abort “#{ $0 } dir [dir]+” unless at_least_one
a @ http://codeforpeople.com/