From: Robert Klemme Date: 2008-06-10T22:35:41+09:00 Subject: Re: Working with arrays 2008/6/10 Clement Ow : > ok, so back to our topic. I have a code where I would like to traverse > into files and folders and then at the same time pass in exceptions and > then after that select the whole load of files to carry out actions like > copying or deleting. But I'll only need help for the traversing of > folders and the exceptions part. Here's my code: I pasted a solution to a this very question on May 30th (Subject "Find files"). Didn't you see it? http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/303503 > src1 = [] > $source.each do |y| > Find.find(y + "/") do |file| The slash is not needed. > src1 << file > $file_exception[i].each do |ex| > src1.delete_if {|x| /#{ex}/ =~ File.basename(file)} This is extremely inefficient since you traverse src1 over and over again. It would be better to test the current file against the exceptions and only put it into src1 if it is not excluded. > end > end > $source is an array of source paths like ["C:/Del", "C:/My Pictures"] > $file_exception is an array of exceptions like [".txt", ".xls"] > > The whole block of code is working fine except that it traverses into > the folders twice, and then parses all the file paths into src1(which is > not what is wanted) Is there any way to improve on my code? See above. Cheers robert -- use.inject do |as, often| as.you_can - without end