From: Jan Svitok Date: 2007-01-15T07:54:04+09:00 Subject: Re: File.rename says file name is to long On 1/14/07, Lovell wrote: Few more notes: 1. you get an array from Dir.entries, so instead of: > test = [] > Dir.chdir("/Users/lem/pictures/") > test[test.length] = Dir.entries("Amber Copy") you can write just (unless you want to append the data, in which case keep the first line, and use test += Dir.entries...): Dir.chdir("/Users/lem/pictures/") test = Dir.entries("Amber Copy") 2. You can rid of the counter using each_with_index: > test.to_s.each do |pics| > counter = 0 > File.rename(pics , "Amber " + counter.to_s + ".jpg") > counter = counter + 1 > end test.each_with_index do |pics, counter| File.rename(pics , "Amber " + counter.to_s + ".jpg") end (NB: counter = counter +1 can be written as counter +=1) 3. if you use #{} in the string, you can get rid of to_s (plus it will be a bit faster): > File.rename(pics , "Amber " + counter.to_s + ".jpg") File.rename(pics , "Amber#{counter}.jpg") 4. IMPORTANT: you most probably want to rename .jpg files only, so either add if pics =~ /\.jpg$/ File.rename(pics , "Amber#{counter}.jpg") end or (the same): File.rename(pics , "Amber#{counter}.jpg") if pics =~ /\.jpg$/ With this approach the skipped files will update the counter, so some numbers will be missing. Therefore it's better to remove the files from test array first, before renaming: test = Dir.entries("Amber Copy").select {|f| f =~ /\.jpg$/}