From: Raul Parolari Date: 2007-11-25T20:48:30+09:00 Subject: Re: Moving files matching Regex Mark Woodward wrote: > There won't be any sub directories except for /sent. I add this to the advice from Stefano: 1) if you just want to find the files in the current directory, then we do not need to use Find (which, as Stefano pointed out, could find files in subdirectories that you may not foresee now..); and we can collect the files in one shot with grep. 2) for precaution, it is better to match the complete file name (just in case there may be other files with that name + an extension, eg like .saved, etc). 3) we could place the method that moves the files into class Array, rather than leaving it in the toplevel. (Consider a param for the destination dir, unless you are sure that it will be always a Constant). class Array def move_to_subdir self.each do |fn| new_loc = SENT_DIR + fn FileUtils.mv(fn, new_loc) unless File.exists?(new_loc) end end end # the main is now 4 lines: fglob= %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x all_files = Dir.glob("*") my_files = all_files.grep(fglob) my_files.move_to_subdir Regards, Raul -- Posted via http://www.ruby-forum.com/.