File.rename says file name is to long

Discussion in 'Ruby' started by Lovell, Jan 14, 2007.

  1. Lovell

    Lovell Guest

    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?
     
    Lovell, Jan 14, 2007
    #1
    1. Advertising

  2. 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/
     
    Vincent Fourmond, Jan 14, 2007
    #2
    1. Advertising

  3. Lovell

    Jan Svitok Guest

    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$/}
     
    Jan Svitok, Jan 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    746
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,245
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    515
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    853
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,898
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page