From: Robert Klemme Date: 2008-05-30T22:09:46+09:00 Subject: Re: Find files 2008/5/30 Clement Ow : > Hi, I have a block of code where I would like to traverese into files > using the Find module and search for files and at the same time have > exceptions to the list of files that I want to eventually select. (These > files are in turn used for copying, moving files) > > My block of code is as follows: > $file_exception[0] = [".xls", ".txt"] > $file_exception[1] = [".txt"] > > $source= ["U:/dest1(1)", "U:/movtest/source", "U:/movtest/new"] > $dest = ["U:/test_1", "C:/MOVTEST/04-08", "C:/DEL/04-08"] > $selections = [*,*,*] > > sd_a=$source.zip($dest,$selections) > > sd_a.each do |sd| > $source, $destination, $selections = sd > d= $d1 > dst= File.join $destination, d > > if $file_exception[i] != nil > $source.each do |y| > Find.find(y + "/") do |file| > src1 << file unless $file_exception[i].each{|x| /#{x}/ =~ > File.basename(file)} > end > end > > else > src1 = $source > end > > i = i + 1 > > src1.each do |folder| > Find.find(folder + "/") do |file| > : > : > 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 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 Kind regards robert -- use.inject do |as, often| as.you_can - without end