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

Batch file rename from excel spreadsheet

I have a 459 files (numbered sequentially) but I would like to rename them with file names contained in an excel spreadsheet e.g. the file currently ending in the number 1 be renamed with the name found in cell A1, the file currently ending in the number 2 be renamed with the name found in cell A2 etc.


Is this possible using automator or any other app?


Thanks

Mac Pro, macOS 10.14

Posted on Dec 21, 2020 6:29 AM

Reply
Question marked as Best reply

Posted on Dec 21, 2020 1:42 PM

From your Dock, click LaunchPad and look for the Other folder icon, containing Automator.

  1. Launch Automator, and select New Document, Application, and click Choose.
  2. Drag and drop the following into the larger right-hand workflow window:
    1. Library : Files & Folders : Ask for Finder Items action
      1. Start at: Desktop
      2. Type: Folders (do not select Allow Multiple Selection
    2. Library : Utilities : Run Shell Script action
      1. Shell: /bin/zsh
      2. Pass input: as arguments
      3. Replace the boiler plate for loop with the code from the Zsh code content below.
    3. Library : Utilities : Run AppleScript action
      1. Replace default AppleScript content with the code from the AppleScript code below
  3. Save the Automator application to your Desktop with an arbitrary name. Double-click to launch it.
  4. Quit Automator.


All the Automator application requires of you is to select the folder containing the files to rename and the CSV that you have located there. It will process the CSV row by row, and rename the files per your CSV entries. When it has finished with the CSV, it will determine whether an error occurred or not and pass the renamed file count appended to either true or false. This is passed on to the AppleScript action, where if successful, a dialog will appear informing you that processing is complete and a file count.



Zsh code


#!/bin/zsh
#

: <<'COMMENT'

Process two column CSV by line, and move (rename) old filename to new filename
in specified directory location. Report files that do not exist.
COMMENT

# zsh shell arrays are 1 based
OLDFILE=1
NEWFILE=2

# drop into selected folder chosen from Ask for Finder Items action
cd "${1}"
CSVFILE="titles.csv"

# a regular array to hold parts of CSV row
typeset -a csv=()

let cnt=0

while read -r line
do
    # split csv line on commas into array elements
    # remove double-quotes from each column entry — if they exist
    csv=("${(@s/,/)line}")
    old_file="${csv[$OLDFILE]:gs/\"//}"
    new_file="${csv[$NEWFILE]:gs/\"//}"
    
    # if old filename exists then rename it
    if [ -e $old_file ]; then
        mv "${old_file}" "${new_file}"
		(( cnt++ ))
    else
        echo "${old_file}" >> ~/Desktop/Errors.txt
		(( cnt-- ))
    fi
    # Only if created on Windows, remove carriage returns from line endings
done < <(perl -lne '$_ =~ s/\015?//g && print;' "${CSVFILE:a:l}")

# if return code from above is 0 then processing was successful and we return
# true with the renamed file count
[[ $? -eq 0 ]] && echo "true $cnt" || echo "false $cnt"



AppleScript code


on run {input, parameters}
	
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
	set temp to text items of (input as text)
	set AppleScript's text item delimiters to TID
	set status to (item 1 of temp) as boolean
	set cnt to (rest of temp) as text
	
	if status then
		display dialog "Processing complete with  " & cnt & "  files renamed." with title "Processing Status"
	else
		display dialog "There was an unknown problem" with title "Processing Status"
	end if
	
	return input
end run



10 replies
Question marked as Best reply

Dec 21, 2020 1:42 PM in response to Andrew Fenner

From your Dock, click LaunchPad and look for the Other folder icon, containing Automator.

  1. Launch Automator, and select New Document, Application, and click Choose.
  2. Drag and drop the following into the larger right-hand workflow window:
    1. Library : Files & Folders : Ask for Finder Items action
      1. Start at: Desktop
      2. Type: Folders (do not select Allow Multiple Selection
    2. Library : Utilities : Run Shell Script action
      1. Shell: /bin/zsh
      2. Pass input: as arguments
      3. Replace the boiler plate for loop with the code from the Zsh code content below.
    3. Library : Utilities : Run AppleScript action
      1. Replace default AppleScript content with the code from the AppleScript code below
  3. Save the Automator application to your Desktop with an arbitrary name. Double-click to launch it.
  4. Quit Automator.


All the Automator application requires of you is to select the folder containing the files to rename and the CSV that you have located there. It will process the CSV row by row, and rename the files per your CSV entries. When it has finished with the CSV, it will determine whether an error occurred or not and pass the renamed file count appended to either true or false. This is passed on to the AppleScript action, where if successful, a dialog will appear informing you that processing is complete and a file count.



Zsh code


#!/bin/zsh
#

: <<'COMMENT'

Process two column CSV by line, and move (rename) old filename to new filename
in specified directory location. Report files that do not exist.
COMMENT

# zsh shell arrays are 1 based
OLDFILE=1
NEWFILE=2

# drop into selected folder chosen from Ask for Finder Items action
cd "${1}"
CSVFILE="titles.csv"

# a regular array to hold parts of CSV row
typeset -a csv=()

let cnt=0

while read -r line
do
    # split csv line on commas into array elements
    # remove double-quotes from each column entry — if they exist
    csv=("${(@s/,/)line}")
    old_file="${csv[$OLDFILE]:gs/\"//}"
    new_file="${csv[$NEWFILE]:gs/\"//}"
    
    # if old filename exists then rename it
    if [ -e $old_file ]; then
        mv "${old_file}" "${new_file}"
		(( cnt++ ))
    else
        echo "${old_file}" >> ~/Desktop/Errors.txt
		(( cnt-- ))
    fi
    # Only if created on Windows, remove carriage returns from line endings
done < <(perl -lne '$_ =~ s/\015?//g && print;' "${CSVFILE:a:l}")

# if return code from above is 0 then processing was successful and we return
# true with the renamed file count
[[ $? -eq 0 ]] && echo "true $cnt" || echo "false $cnt"



AppleScript code


on run {input, parameters}
	
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
	set temp to text items of (input as text)
	set AppleScript's text item delimiters to TID
	set status to (item 1 of temp) as boolean
	set cnt to (rest of temp) as text
	
	if status then
		display dialog "Processing complete with  " & cnt & "  files renamed." with title "Processing Status"
	else
		display dialog "There was an unknown problem" with title "Processing Status"
	end if
	
	return input
end run



Dec 21, 2020 9:00 AM in response to Andrew Fenner

I would suggest that you insert a column before Column A in your Excel Spreadsheet and populate it with the ordered, original filename that is to be renamed by the corresponding filename in the (now) second column. Neither column should contain any punctuation marks in names. Then export the two columns as a UTF-8 text, unquoted, CSV (comma separated values) text file without headings.


Now you have a CSV where far simpler code can readily access each row of the CSV and perform the rename from its contents.


Are the 459 rename candidate files in a single, flat folder, or contained in a hierarchy of sub-folders within a parent folder? What is a sample of the original filename and the intended renamed filename? Is the CSV being generated on a WIndows computer, or on a Mac?

Dec 21, 2020 9:18 AM in response to VikingOSX

Thanks for this. They are all in a flat folder. An example of the filenames:


Original: Voluntary Titles - Merged_Page_001.jpg

Renamed: 001_Blessed_are_they_who_listen_not_to_evil_counsel.jpg


Original: Voluntary Titles - Merged_Page_002.jpg

Renamed: 002_Give_ear_to_my_words.jpg


etc.


The CSV will be generated on a Mac (but it can be done in Windows if that is preferable).



Dec 21, 2020 11:14 AM in response to Andrew Fenner

Once you have the CSV done, let me know, and i will provide you an Automator solution that prompts for the folder containing these files, and the script that I provide will do the renaming for you. If you don't want a separate prompt selection for the CSV file, just place it in the same folder as the files, and provide its name to me.

Dec 22, 2020 4:29 AM in response to VikingOSX

That is absolutly brilliant and worked a treat! Thank you so much. I know automater is a brillinat app, but I have never been able to get my head around it.


Can I ask you another question, or should I post it as a seperate post? It is this:


I have 459 jpegs which I want to add as artwork to the ID3 tags if 459 mp3's. Each Jpeg is different. I have used Picard for labeling commercial tracks, but these are all self produced mp3's and artwork. I have downloaded kid3 and can see how to do it one track at a time, but is there anyway of doing it automatically in either kid3 or automater?


As I say, if it is better to ask the question in a different post, I am more than happy to do that.


Thank you so much for your help.

Batch file rename from excel spreadsheet

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