Robert Klemme
10/22/2006 1:04:00 PM
Li Chen wrote:
> Hi folks,
>
> I write two srcipts to check files in a given directory. Version 1 uses
> regular expression and works but I cann't find the size for each file.
> Version 2 uses Ruby built-in File.file? method but it doesn't work at
> all. I wonder what is going on with my scripts?
So you want to sum sizes of all files in a directory hierarchy whose
names match a certain pattern. The pattern you use cannot be used with
Dir[] for filtering. So you better use find:
sum = 0
Find.find(path) do |f|
sum += File.size(f) if
/(\w+|d+).(\d{3,})/ =~ File.basename(f) && File.file?(f)
end
Note, your regexp might not match what you actually think it matches.
At the moment you match all file names that contain (!) at least a
single digit or word character followed by any character and then at
least three digits. I am guessing here but do you maybe rather want all
files that have a purely numeric file extension? In that case this
regexp would be better
/\.\d+$/
Regards
robert