You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Batch file rename from CSV file

Hello! I see this question has been asked several times in this community, but I can't seem to figure out why I'm not able to run Automator successfully.


I have 183 audio files. I would like to rename them. I created a CSV file with two columns—column A is current file names and column B is intended file names.


I have followed the instructions in this earlier thread by @VikingOSX, including putting the CSV file in the audio file folder.


The output I get when running the Automator app I've created is "Processing complete with 0 files renamed." I am running Sequoia 15.1.1


Any guidance would be incredibly appreciated!


#!/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


MacBook Pro 14″

Posted on Dec 17, 2024 7:10 PM

Reply
1 reply

Dec 18, 2024 2:25 AM in response to arrohansu

The Run AppleScript action requires the following change so the display dialog does not blow up and the vertical ellipsis implies original code without changes.

use scripting additions

on run {input, parameters}
⋮
end run



For the Run Shell Script action, verify that you have set Shell: [ /bin/zsh ] and the Pass Input: [ as arguments ]. That allows the folder path that you selected in the preceding Ask for Finder Items action to get passed into the Run Shell Script action.


The Run Shell Script as I originally posted expects a comma delimiter (without surrounding spaces) between column A and column B in the CSV file. There can be no commas in the filenames. If you are using a different column delimiter, the single line of code in the script where this can be changed is documented.


The code that you posted, with the addition shown above for the Run AppleScript action, worked perfectly in Automator on Sequoia v15.2 without any other change when using a comma separator in my CSV test file.



Batch file rename from CSV file

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