Alle mercoled=C3=AC 14 marzo 2007, Sam Fent ha scritto:
Hi Stefano (anzi, 'ciao!'),
Thank you very much -- that has helped me tremendously. I have one
further question though. What if, instead of the number "9", I wanted to
copy the file with the HIGHEST number.
For example, my files might be
example1.txt
example2.txt
test1.txt
test2.txt
test3.txt
If I wanted to change example2.txt to example-final.txt, and test3.txt
to test-final.txt, how could I iterate throught the files to find the
file with the highest number?
Grazie mille per qualsiasi aiuto,
Sam
This should work:
require 'find'
require 'set'
require 'file_utils'
=46ind.find('.') do |f|
# consider only directories, we'll take care of files later=09
if File.directory?(f)
# create a Set containing the names of the files in the directory f [1]
entries=3DSet.new(Dir.entries(f).reject{|e| File.directory?(e)})
#separate the files basing on their basename (the part before -number) =
[2]
names=3Dentries.classify{|e| e.match(/^.*-\d+$/)[1]}
# for each of the basenames
names.each_value do |v|
# sort the set basing on the number and select the last
name=3Dv.sort_by{|n| n.match(/-(\d+)/)[1].to_i}.last
# copy it to the new name. name is relative to f, so we prepend it
FileUtils.cp "#{f}#{name}", "#{f}#{name.sub(/-\d+$/, '-final')}"
end
end
end
The line marked by [2] creates a hash of sets, each of them containing the=
=20
files with the same basename. This method is the only reason to use a set i=
n=20
line [1], instead of keeping the array. For instance, if the directory f=20
contains the files
a-1
a-2
a-3
b-1
b-2
c-1
c-2
c-3
c-4
the result of line [2] will be a hash with keys 'a', 'b' and 'c'. The keys =
'a'=20
will contain a set with elements 'a-1', 'a-2', 'a-3', and so on for the oth=
er=20
keys.
Stefano