From: Li Chen Date: 2006-10-23T06:16:47+09:00 Subject: Re: What is wrong with my code Robert Klemme wrote: ... > 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 > ... Hi Robert, Thank you very much for the code. Based on what I understand and what I need I make some changes. But I still have some questions:1) How do I factor the print or format codes here? 2) The outputs of the file are in reverse order how do I print them out in this format; xxx.001, xxx.002,...,xxx.026 3) what is the purpose of File.basename here? Thanks, Li #dir6.rb find the file number in a folder require 'find' path='I:/Common/Gao/Notebooks/Flow/OT1/OTI-4' file_number = 0 Find.find(path) do |f| #sum += File.size(f) if # /(\w+|d+).(\d{3,})/ =~ File.basename(f) && File.file?(f) #puts f if /(\w+|d+).(\d{3,})/ =~ File.basename(f) && File.file?(f) if File.file?(f) && f=~/(\w+|d+).(\d{3,})/ print f,"\t" printf("%10s %10s", File.size(f),'byte' ) puts file_number+=1 end end puts file_number ###screen output I:/Common/Gao/Notebooks/Flow/OT1/OT1-typing-3/OT1-3.028 4796348 byte .... I:/Common/Gao/Notebooks/Flow/OT1/OT1-typing-3/OT1-3.001 11877548 byte 28 -- Posted via http://www.ruby-forum.com/.