Forum: Ruby File.rename says file name is to long

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ea905398d356c5d6a10bba35b8d16f0c?d=identicon&s=25 Lovell Mcilwain (lmcilwain)
on 2007-01-19 16:30
(Received via mailing list)
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 (http://www.ruby-doc.org/core/classes/File.html#M002591)

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?
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jan Svitok (Guest)
on 2007-01-19 16:30
(Received via mailing list)
On 1/14/07, Lovell <lovell.mcilwain@gmail.com> 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$/}
35b0b4029fd4387842ec88a8e99d84de?d=identicon&s=25 Jason Mayer (Guest)
on 2007-01-19 16:30
(Received via mailing list)
>
> 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?
Ea24c17719a975fb38c107a60f4b3802?d=identicon&s=25 Vincent Fourmond (Guest)
on 2007-09-25 22:30
(Received via mailing list)
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 ;-) ! Removing the unnecessary to_s should do the trick.
Cheers,

  Vince
B6404d14122238d1c5905e39512f2d7d?d=identicon&s=25 Howard Brooks (howard_brooks)
on 2016-11-26 23:04
Long Path Tool is the perfect utility that can solve all the long path
issues. Download this software online.
3305c73b68c0ff68021506649ef18959?d=identicon&s=25 Anton Dmitriy (anton_dmitriy)
on 2016-11-27 20:07
Please use Dulpicate files deleter,it is very simple to use. But make it
sure to keep the important files in back up. Thanks
This topic is locked and can not be replied to.