Find from array and move

Hi,

You could use a glob (file pattern) to do this:
http://ruby-doc.org/core-1.9.3/Dir.html#method-c-glob

For example, if you are looking for every file with the name "x.txt" in
the directories "C:/a/" and "C:/b/", you can write

Dir['C:/{a,b}/x.txt']

This returns an array of the files found.

If you also want to include subdirectories, write

Dir['C:/{a,b}/**/x.txt']

You can then move the file with FileUtils.move (you'll have to include
the fileutils library first).

As an example code:

···

####
require 'fileutils'

search_dirs = ['C:/a/', 'C:/b/']
search_files = ['x', 'y', 'z']
target_dir = 'C:/z/'

# todo: escape special characters like "{" and "}"
search_pattern = "{#{search_dirs.join ','}}/**/"
search_files.each do |filename|
  found = Dir["#{search_pattern}/#{filename}.txt"]
  case found.length
  when 1
    FileUtils.move found.first, target_dir
  when 0
    puts "File #{filename}.txt not found"
  else
    puts "File #{filename}.txt found more than once, didn't move"
  end
end
####

--
Posted via http://www.ruby-forum.com/.