File.rename says file name is to long

Hey guys,

I am trying to rename a list of files, pictures to be exact, so that
they don't show up as some random/unknown name.

I am getting an error

···

===================

./rename.rb:20:in `rename': File name too long -
...rename.rbunknown-1.jpgunknown-10.jpgunknown-11.jpgunknown-12.jpgunknown-13.jpgunknown-14.jpgunknown-15.jpgunknown-16.jpgunknown-17.jpgunknown-18.jpgunknown-19.jpgunknown-2.jpgunknown-20.jpgunknown-21.jpgunknown-22.jpgunknown-23.jpgunknown-24.jpgunknown-25.jpgunknown-26.jpgunknown-27.jpgunknown-28.jpgunknown-29.jpgunknown-3.jpgunknown-30.jpgunknown-31.jpgunknown-32.jpgunknown-33.jpgunknown-34.jpgunknown-35.jpgunknown-36.jpgunknown-37.jpgunknown-38.jpgunknown-39.jpgunknown-4.jpgunknown-40.jpgunknown-41.jpgunknown-42.jpgunknown-43.jpgunknown-44.jpgunknown-45.jpgunknown-46.jpgunknown-47.jpgunknown-48.jpgunknown-49.jpgunknown-5.jpgunknown-50.jpgunknown-51.jpgunknown-52.jpgunknown-53.jpgunknown-54.jpgunknown-6.jpgunknown-7.jpgunknown-8.jpgunknown-9.jpgunknown.jpg
or Amber 0.jpg (Errno::ENAMETOOLONG) from ./rename.rb:20 from
./rename.rb:14

when I try to run my script but I can't seem to figure out the error.

From what I can see , its turning my list of names into one long string

the its telling me the file name is to long. Based on what I read in
the Ruby Doc website (http://www.ruby-doc.org/\) specifically on
File.rename (class File - RDoc Documentation)

It tells me the syntax I should use is

File.rename(old_name, new_name)
example: File.rename("afile", "afile.bak")

===================
The code I used to write this is as follows:

===================

test =

Dir.chdir("/Users/lem/pictures/")

test[test.length] = Dir.entries("Amber Copy")

test.to_s.each do |pics|
counter = 0
File.rename(pics , "Amber " + counter.to_s + ".jpg")
  counter = counter + 1
end

puts "Done!"

===================

I have a feeling its something simple but I just can't seem to grasp
what it is, can anyone drop me a hint?

Lovell wrote:

test =

Dir.chdir("/Users/lem/pictures/")

test[test.length] = Dir.entries("Amber Copy")

test.to_s.each do |pics|

  test.to_s is a single string containing the concatenation of all the
names of the files in the "Amber Copy" directory. See:

irb(main):001:0> t = [1,2]
=> [1, 2]
irb(main):002:0> t.to_s
=> "12"
irb(main):003:0> t.to_s.each do |a| p a end
"12"
=> "12"

I have a feeling its something simple but I just can't seem to grasp
what it is, can anyone drop me a hint?

  Indeed :wink: ! Removing the unnecessary to_s should do the trick. Cheers,

  Vince

···

--
Vincent Fourmond, PhD student
http://vincent.fourmond.neuf.fr/

test.to_s.each do |pics|
counter = 0
File.rename(pics , "Amber " + counter.to_s + ".jpg")
  counter = counter + 1
end

puts "Done!"

Um, haven't played with it, but should the counter = 0 be inside your loop?

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$/}

···

On 1/14/07, Lovell <lovell.mcilwain@gmail.com> wrote: