Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > File.rename says file name is to long

Reply
Thread Tools

File.rename says file name is to long

 
 
Lovell
Guest
Posts: n/a
 
      01-14-2007
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?

 
Reply With Quote
 
 
 
 
Vincent Fourmond
Guest
Posts: n/a
 
      01-14-2007
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

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

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      01-14-2007
On 1/14/07, Lovell <(E-Mail Removed)> 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$/}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
long long and long Mathieu Dutour C Programming 4 07-24-2007 11:15 AM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Dell says no & Acronis says maybe sysprep utility ( Re: Anyone use Acronis Drive Image 7.0? Bobby Fischler Computer Support 0 07-24-2004 12:12 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments