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″