From: Stefano Crocco Date: 2007-03-15T00:37:49+09:00 Subject: Re: Copying files and changing their names Alle mercoled狸 14 marzo 2007, SamF ha scritto: > Hi all, > > I have a directory with numerous folders, each of which contains > numerous folders and/or files. > > Each of the files in each of these folders has a number. I want to > iterate through all these files, and, for every file that ends in a > '[filename]-9', I want to COPY the file and rename it > '[filename]-final'. > > Is there a simple way to do this in Ruby? > > Any help much appreciated! > Sam NOT TESTED - make a trial with some useless directory before using it on real data require 'find' require 'fileutils' Find.find('.') do |f| FileUtils.cp(f,f.sub(/-9$/, '-final')) if File.file?(f) and f.match /-9$/) end Find.find calls the block passing the name of each file or directory it contains, recursively. In the block, we use the FileUtils.cp method to copy f to the file whose name is obtained by rmoving the final -9 and adding -final, but only if the name is the name of a file (not of a directory) and if the name ends with -9. I hope this helps Stefano