From: Robert Klemme Date: 2008-05-30T22:13:42+09:00 Subject: Re: Find files Sorry, hit "send" too early. 2008/5/30 Robert Klemme : > 2008/5/30 Clement Ow : >> But apparently, the file exception doesnt seem to work.. It doesnt just >> exclude the file exceptions that has the file extentions .txt and .xls, >> it excludes the whole list of files in the first 2 source paths, and >> only executes the files in the 3rd source path. I presume it should be >> something really wrong with the: >>>> src1 << file unless $file_exception[i].each{|x| /#{x}/ =~ File.basename(file)} >> that I cant quite figure(i tried figuring for days!) Help anyone? =) You can make your life much simpler by first separating the finding of files from what you do with them: use a block for the "do" part. Now you just need a method that finds files, honors exclusions and emits from and to names. Then you can do anything including moving, copying and deleting in the block. Matching is often abstracted by operator === (see case statements for example). So this is a pretty generic thing for determining matches or mismatches. Regular expressions do also implement it. > You can do this: > > #!/bin/env ruby > > require 'find' > > def my_find(source, dest, exclude) > Find.find source do |file| > unless exclude === file > dest_file = file.dup > dest_file[0...source.length]=dest > yield file, dest_file > end > end > end > > my_find ".", "foo", /\.(?:xls|txt)$/ do |from, to| > print from, " -> ", to, "\n" > # FileUtils.cp from, to > # FileUtils.mv from, to > end Cheers robert -- use.inject do |as, often| as.you_can - without end