Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apple automator / script to rename images from folder using a CSV file

Hi Guys!


I hope all of you are doing well and that someone can help me with my problem.


I don’t have much knowledge when it comes to automator or terminal commands (if that’s the case here) but I’m preety sure that I can understand and implement the solution. Ok, here is the problem I have:


I have a folder with 2,000 image files. I need to change the names of about third of them. I have made a simple CSV sheet; column A has the existing names of the files I want to replace that you can find on the image folder, and column B has the names that I’m looking to replace them with. The remaining files should remain untouched, so I’m only looking to change the ones from the excel file, column A.


I guess its relatively simple but I have no clue how to do it. I did some search about it but was not able to find a solution.


Thanks again!

Mac mini, iOS 10.3.3

Posted on Sep 2, 2018 8:47 AM

Reply
Question marked as Best reply

Posted on Sep 3, 2018 12:20 AM

OK. The script is now executing but it is only working and removing the trailing '?' if I go to each file and change its type to image again. I then was able to locate the problem – there was an invisible special character that was showing at the end of each filename after renaming the files from the CSV.


Now that I know that its adding this invisible special character its super easy to remove, and once remove no more trailing '?'!! So problem solved!


VikingOSX, I really would like to thank you a lot for solving my problem. Thanks for taking the time to help! Really appreciate it! Maybe this thread will also help other people.

Similar questions

13 replies
Question marked as Best reply

Sep 3, 2018 12:20 AM in response to VikingOSX

OK. The script is now executing but it is only working and removing the trailing '?' if I go to each file and change its type to image again. I then was able to locate the problem – there was an invisible special character that was showing at the end of each filename after renaming the files from the CSV.


Now that I know that its adding this invisible special character its super easy to remove, and once remove no more trailing '?'!! So problem solved!


VikingOSX, I really would like to thank you a lot for solving my problem. Thanks for taking the time to help! Really appreciate it! Maybe this thread will also help other people.

Sep 2, 2018 11:00 AM in response to MacGuy1900

Header, or no header in the .csv file?


The following Bash script will take the .csv file as a command-line argument, and using a comma as the .csv separator character, rename the specified image files to the new filenames. The Bash script would reside in the same folder as the image files. This particular example does not handle the header in the .csv file.


Steps:

  1. Copy/paste the following Bash script into a plain text editor, and save as csv.sh (arbitrary name) in the image folder.
  2. Mark the script executable:
    chmod +x ./csv.sh
  3. Run the script in the Terminal
    ./csv.sh ./your.csv


#!/bin/bash


# Read in a two-column, comma-separated values .csv file and
# rename col1 filename to col2 filename


# usage: ./csv.sh ./this.csv
# simply exit if no file argument supplied on command-line
[[ ! $# ]] && exit 1


for f in "$@"
do
    # if the extension of the input file is not csv, then exit
    [[ ! "${f##*.}" = csv ]] && echo "Not a CSV format file" && exit 1


    while IFS=, read old new
    do
        mv "$old" "$new"
    done < "$f"
done
exit 0

Sep 3, 2018 12:50 AM in response to MacGuy1900

Header, or no header in the .csv file?


The following Bash script will take the .csv file as a command-line argument, and using a comma as the .csv separator character, rename the specified image files to the new filenames. The Bash script would reside in the same folder as the image files.


Steps:

  1. Copy/paste the Bash script into a plain text editor, and save as csv.sh (arbitrary name) in the image folder.
  2. In the Terminal, change into the specific directory in your home directory:
    cd ~/foldername
  3. Mark the script executable:
    chmod +x ./csv.sh
  4. Run the script in the Terminal
    ./csv.sh ./your.csv

Bash script:

#!/bin/bash


# Read in a two-column, comma-separated values .csv file and
# rename col1 filename to col2 filename


# usage: ./csv.sh ./this.csv
# simply exit if no file argument supplied on command-line
[[ ! $# ]] && exit 1


for f in "$@"
do
    # if the extension of the input file is not csv, then exit
    [[ ! "${f##*.}" = csv ]] && echo "Not a CSV format file" && exit 1


    while IFS=, read old new
    do
        mv "$old" "$new"
    done < "$f"
done
exit 0

Sep 2, 2018 2:08 PM in response to MacGuy1900

Clean up the literal double-quotes on your renamed files. Once you have done this, then in the same folder as your renamed images, run this one-liner in the Terminal:


for f in *.jpg\?;do mv "$x" "${x%.*}.jpg";done


This will make a list of all .jpg? filenames, and then rename each one with a correct (.jpg) extension. You will need to do this for each affected image type with a trailing question mark.


When parsing .csv files, it is important to know in advance for proper programmatic processing:

  1. if the .csv originated from MS Windows, or was saved on a Mac with CRLF line endings with a Windows destination in mind. A .csv should be saved as UTF-8 plain text, and not the Excel default of UTF-16.
  2. If the .csv entries are quoted or not. If not, do any entries contain spaces?
  3. What is the column separator character. Usually, but not always a comma. It should not be within data elements.
  4. Does the .csv contain a heading row.

Sep 2, 2018 12:10 PM in response to MacGuy1900

The script needs to be saved as plain text, not rtf if you used TextEdit. Is the first line of the script #!/bin/bash?


Sorry for the redundant post. The hosting software was not showing the first response, even after a browser refresh.


I believe you will also get that error if you saved the Excel .csv file as UTF-16, as it places a byte-order-mark (BOM) as the first character of the .csv, and this will explode the shell script that is expecting UTF-8 plain text .csv.

Sep 2, 2018 12:36 PM in response to VikingOSX

Its working!


Two minor comments that are really no big deal. The last line from the excel file is not working and seems like its skipping it, but that I can do manually. The other issue is that the rename of the image file is adding " at the beginning and end and also a ? (for example: "FILENAME"?). You think its possible to execute this script without adding the "" and ?


Thank you again!

Sep 2, 2018 1:03 PM in response to MacGuy1900

Can you screencapture (shift+command+4) and drag over the first five lines of your Excel file, that you should be able to use Quick Look (spacebar) to view? Use the camera icon in this toolbar to post that screencapture image back here. Also, screencapture one of the filenames from in the Terminal, or where you see the trailing '?'.


There is nothing in the shell script that explicitly places quotes around, or forces a trailing ? character on your filenames. The double-quotes that you see in the script are there to manage filenames with white-space. On the other hand, did you generate an Excel .csv with quoted entries, which this script would dutifully consider as legitimate parts of the filenames?


Did this Excel .csv originate on Windows, or Mac?

Sep 2, 2018 1:25 PM in response to VikingOSX

Okay, so regarding the quotes – I forgot to mention on all files I have a comma in some place of the filename. I tested it without the comma and I did NOT get the double quotes. However, I can remove them using some rename batch program I use, so its not a big deal.


The only problem is the adding of the trailing ? at the end (doesn’t matter if file has a comma or not). As I cannot remove it unless I go file by file and remove it manually. Then system asks me:


Are you sure you want to change the extension from “.jpg"” to “.jpg"”?


I tested it with a clean .csv with a simple rename of “111.jpg” to “222.jpg” (without the quotes of course). It worked fine expect the trailing ? at the end.


Also, I’m using a Mac.

Yes, if you have a solution for the trailing ? at the end please let me know!

Thanks!

Sep 2, 2018 2:24 PM in response to VikingOSX

Thank you for the quick follow-up! Again!


There are no spaces in the filename, only underscore instead (like: aaa_bbb,_ccc.jpg). Also, the CSV does not contain a heading row.


I tired to insert this line as you indicated but got this message:


mv: rename to .jpg: No such file or directory


not sure why as I removed the “” before doing so.


Any idea? So close to fix it and thank you again for all the help!

Apple automator / script to rename images from folder using a CSV file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.