Here is a revised script that you can read more about in its comments. It does not skip any lines in the CSV and assumes that the CSV is in the same folder as the files to rename. Change the name of the CSV in this script for yours.
Using the following CSV contents, I tested the revised script on macOS Sonoma 14.3.1.
"6BR1L,10033410-410.txt","6BR1L,10033410-a.txt"
"6BR1L,10033410-411.txt","6BR1L,10033410-b.txt"
"6BR1L,10033410-412.txt","6BR1L,10033410-c.txt"
"6BR1L,10033410-413.txt","6BR1L,10033410-d.txt"
"6BR1L,10033410-414.txt","6BR1L,10033410-e.txt"
"6BR1L,10033410-415.txt","6BR1L,10033410-f.txt"
and the revised script itself:
#!/bin/zsh
: <<"COMMENT"
Process a CSV where separator characters may exist in the filename and
by using a regular expression capture on the double-quoted columns in the
CSV, we can ignore the separator character itself. Assumption for this script
is that the CSV file is in the same folder as the files to process.
Usage: ./csvtest.zsh folderpath
COMMENT
CSVFILE="6B.csv"
# drop into selected folder chosen from Ask for Finder Items action
cd "${1}"
let cnt=0
while read -r line
do
# use a regular expression to capture quoted content on either side of the
# arbitrary CSV separator character. This will result in a capture array
# in the match variable. The dot between the captures is the real
# separator character which becomes irrelevant to processing.
[[ ${line} =~ "(\".*\").(\".*\")" ]] || continue
# now we can remove the double-quotes around the filenames so they
# will test for existence and rename properly
old_file="${match[1]:gs/\"//}"
new_file="${match[2]: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