Batch rename using CSV & such

Discussion in 'Mac Programming' started by bobbybill, Sep 15, 2012.

  1. bobbybill, Sep 15, 2012
    Last edited: Sep 15, 2012

    bobbybill macrumors newbie

    Joined:
    Sep 15, 2012
    #1
    UPDATE: I've figured out how to run the .rb. But I'm hung up on an error code when I'm in the terminal:
    "There was an error reading SKU.csv: No such file or directory - SKU.csv
    Exiting..."



    Hello:

    Check out this link to an old post on MacRumors--

    http://forums.macrumors.com/showthread.php?t=782851


    I want to do the same thing but have not been able to figure out the steps to take--according to the original responder--or whether the new Lion OS has any significant differences compared to the original post.

    Thanks for your help!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    The "old post" you've linked to is a member page, not a post, or even a thread. It's a bit much to search that member's entire post history to find something that might be CSV-related.
     
  3. bobbybill thread starter macrumors newbie

    Joined:
    Sep 15, 2012
    #3
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Post the exact commands you used in Terminal (copy and paste them), along with the error messages.

    If you used double-click on the .rb, copy and paste the complete text from the Terminal window.


    The URL with the .rb file is here:
    http://www.roguepenguin.net/PFID2SKU/

    There is a downloadable "SKU.csv" file also available there. Did you download that file, too? Where did you put it?


    I'm trying to pin down exactly what you did. The command-line must be accurate and specific. Since the original thread didn't include any examples of the command-line, it tells you to read the start of the .rb file for information on how to customize the .rb file. Did you do that? Exactly what did you do as a result? The .rb file clearly needs to be customized, so exactly how did you customize it?

    We can't see your screen, so you have to tell us exactly what you did, for each customization and every command-line you entered. For example, you haven't told us whether you typed in a command-line, or whether you used double-click in Finder.
     
  5. bobbybill, Sep 15, 2012
    Last edited by a moderator: Sep 15, 2012

    bobbybill thread starter macrumors newbie

    Joined:
    Sep 15, 2012
    #5
    What I tried...

    Background: To test whether it would work on my own data..I used the data provided in the old post. I have a folder named 'namechangetest' with 2 subfolders: 'PFIDimages' & 'SKUimages', and 2 documents (PFID2SKU.rb & SKU.csv) Within the 'PFIDfolder' are 5 images (PFIDimages)that correspond to values in 'A:A' column.

    First I tried double-clicking the .rb file. That opened the file in xcode by default. After that I tried to do this in the terminal:

    Code:
     Nicks-MacBook-Pro:~ nickburggraf$ ruby ~/desktop/Namechangetest/PFID2SKU.rb 
    which gave me:

    Code:
     There was an error reading SKU2.csv: No such file or directory - SKU2.csv
    Exiting...
    
    Then I tried copying and pasting into the terminal--all the lines in the code--see attachment.
     

    Attached Files:

  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Try this: copy and paste the following into a Terminal window:
    Code:
    cd ~/Desktop/namechangetest
    ruby ./PFID2SKU.rb
    
    Describe what happens.
     
  7. kryten2, Sep 15, 2012
    Last edited: Sep 15, 2012

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    I tested the script. Made some images. Perhaps it makes things clearer.
     

    Attached Files:

  8. bobbybill thread starter macrumors newbie

    Joined:
    Sep 15, 2012
    #8
    Attempted to copy-paste, as from CHOWN

    I received this result:

    Code:
    Last login: Sat Sep 15 17:59:07 on ttys000
    Nicks-MacBook-Pro:~ nickburggraf$ cd ~/Desktop/Namechangetest
    Nicks-MacBook-Pro:Namechangetest nickburggraf$ ruby ./PFID2SKU.rb
    009-07-0414 Weber Smokey Joe.jpg,030-04-1821' does not appear to be a valid mapping
    Exiting...
    
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Refer to post #5 in the original thread.

    If that doesn't answer your question, then you need to post the actual file you're using as your "SKU.csv".
     
  10. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    Don't know if this is the result you expected as your SKU has no extension.
     

    Attached Files:

  11. bobbybill thread starter macrumors newbie

    Joined:
    Sep 15, 2012
    #11
    Please stand by...

    I am going to post everything that I have when I get home--just about to leave work. 30 min.

    Thank you for all your help..Looking forward to figuring it out (or should I say, having one of you figure it out). Thanks.
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    There's only one error message in the Ruby script that says "invalid mapping" (hilited in red):
    Code:
          if tokens.size == 2                             #the split line should have 2 substrings/tokens
            pfid_name = tokens[0].strip                   #remove any extra whitespace on first token
            sku_name = tokens[1].strip                    #remove any extra whitespace on second token
            if mapping.has_key?(pfid_name)                #make sure we have not already encountered this PFID name 
              raise "'#{pfid_name}' already exists (this is a duplicate)"     #throw an error if we have
            else
              mapping[pfid_name] = sku_name               #map the PFID to the SKU name
            end
          else                                            #if there are not two substrings/tokens, raise an error
            [COLOR="Red"]raise "The line '#{line}' does not appear to be a valid mapping"[/COLOR]
          end
    
    This code is part of the initial parsing, so AFAICT it's just splitting strings and looking for 2 parts. It's not actually looking for files yet.

    I note that the words "The line" are missing from the posted error message, so I suspect some error in the data. For example, a lone CR might cause the line-parsing code to malfunction (I don't know how Ruby parses lines), effectively treating two lines separated by a single CR as a single line that looks like "id1,sku1 CR id2,sku2". This would fit both the message output and the code logic, but it's just a guess; I'll wait to get the actual data file.
     
  13. bobbybill thread starter macrumors newbie

    Joined:
    Sep 15, 2012
    #13
    The Data

    Here is everything I have--that I am supposed to have?

    Contained in the .zip:
    SKU.csv (4 entries)
    PFID2SKU.rb
    pfidimages folder w/ 4 images
    skuimages--empty
     

    Attached Files:

  14. kryten2, Sep 15, 2012
    Last edited: Sep 15, 2012

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #14
    Your csv file is malformed as chown33 suspected.
     

    Attached Files:

  15. bobbybill thread starter macrumors newbie

    Joined:
    Sep 15, 2012
    #15
    Malformed?!

    Oh no! Do you know the way to form it correctly? I attempted a new .csv from scratch, but yielded the same result.
     
  16. chown33, Sep 15, 2012
    Last edited: Sep 15, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    What program are you using to produce the .csv file? It may have an option to control line-endings. The kind needed here is often called "Unix style" line-endings.

    If you can't control the line-ending style, you can edit the .csv file after it's been produced, and change the line-endings. A simple way to do that is with TextWrangler. After installing it (drag-and-drop) and launching it (skip registration for now, also skip installing command-line utilities), open your .csv file.

    Without changing any preferences, look at the bottom of the window. There should be a few assorted popups, and one of them should say something like "Classic Mac (CR)". Click that and choose "Unix (LF)". Save. The newly-saved file should now have Unix-style line-endings, so run the command-line as previously shown, and describe the results.

    There are other ways to convert line-endings, including with a command-line, but TextWrangler is a useful tool so you might as well have it handy. There are almost certainly other text editors that can convert line-endings, but TextWrangler is definitely free (I don't know about others), easy to install (not always the case), and the line-ending conversion is right there after a default install.


    p.s. We programmers often use "malformed" only in the sense "not what was expected" or "not in the conventional way". It's almost never in the sense "something diabolical or horrific has happened" (that would be "grossly malformed"). Since most programs that produce text on Mac OS X do so with Unix-style line-endings, "malformed" only means "not using the conventional line-endings".

    EDIT

    Duh, TextWrangler is also free on the Mac App Store.
    http://itunes.apple.com/us/app/textwrangler/id404010395?mt=12
     
  17. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #17
    Sorry, I didn't reply earlier but I seem to have dozed off after my last post(5 o'clock in the morning here). Thanks chown33 for the TextWrangler guidance and the p.s. ;)

    @bobbybill

    I guess the command line chown33 mentioned would be :

    Code:
    tr '\015' '\n' < input_file > output_file 
     
  18. bobbybill thread starter macrumors newbie

    Joined:
    Sep 15, 2012
    #18
    Resolution!

    It works! Thank you all for your assistance! I am obliged to each of you.
    God bless.
     

Share This Page