20,000 files to rename - Automator / CSV

Hello,

Sorry for bothering the community with something already approached multiple times.... but... I tried many scripts from this forum and many other alternatives, but none of them works... Even Chat Gpt can't find a working script.


I followed the VikingOSX tutorial to rename a bunch of files with automator, from a CSV file.

I tried this one : Batch file rename from Excel spreadsheet - Apple Community

It says that it's works, but the files still the same and an error .txt file appears on the desk.


Right now, i try to make it work on a few files. Here is the setup.



I have more than ~20,000 files to rename so i have to make it work ;)

I'm on an iMac with Mojave 10.14.6.


Thank you so much for your time and help.

iMac Pro (2017)

Posted on Jul 20, 2023 5:33 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 20, 2023 11:13 AM

Every one of these scripting/Automator solutions are custom to the individuals need and no script is universal in its ability to process different file renaming problems. ChatGPT is worthless for this approach too.


The solution that I wrote for the OP in the linked post split CSV by commas and I see semi-colons in your content, so of course the script needs revision for your renaming effort.


Using your before and after file naming convention in a newline terminated CSV with semi-colon separators and no headings, the following script successfully renames to your filename goal.


Before:



After:




Follow the exact steps for creating the Automator solution in Batch file rename from Excel spreadsheet - Apple Community. Replace the contents of the Run Shell Script action with this revised 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="testrename.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 semi-colons into array elements
    # remove double-quotes from each column entry — if they exist
    csv=("${(@s/;/)line}")
    old_file="${csv[$OLDFILE]:gs/\"//}.jpg"
    new_file="${csv[$NEWFILE]:gs/\"//}.jpg"
    
    # 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"


Tested: macOS 13.4.1 (c).


Similar questions

11 replies
Question marked as Top-ranking reply

Jul 20, 2023 11:13 AM in response to zirio13

Every one of these scripting/Automator solutions are custom to the individuals need and no script is universal in its ability to process different file renaming problems. ChatGPT is worthless for this approach too.


The solution that I wrote for the OP in the linked post split CSV by commas and I see semi-colons in your content, so of course the script needs revision for your renaming effort.


Using your before and after file naming convention in a newline terminated CSV with semi-colon separators and no headings, the following script successfully renames to your filename goal.


Before:



After:




Follow the exact steps for creating the Automator solution in Batch file rename from Excel spreadsheet - Apple Community. Replace the contents of the Run Shell Script action with this revised 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="testrename.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 semi-colons into array elements
    # remove double-quotes from each column entry — if they exist
    csv=("${(@s/;/)line}")
    old_file="${csv[$OLDFILE]:gs/\"//}.jpg"
    new_file="${csv[$NEWFILE]:gs/\"//}.jpg"
    
    # 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"


Tested: macOS 13.4.1 (c).


Jul 21, 2023 5:53 AM in response to zirio13

When you first drag/drop that Run Shell Script action into the Workflow window, and change the pass input: selection from to stdin to as arguments, it automatically inserts the unwanted code:


for f in "$@"
do
	echo "$f"
done


You must remove all of that default code before you copy/paste the code I provided above into the Run Shell Script action.

Mar 27, 2024 6:12 AM in response to ChewyyCheezyy

You should consider using extensions with filenames in both columns of a CSV. Otherwise, you would need to borrow the extension from the first column filename and append that extension to the newfile name. If no extension on first column filenames, then the script should quit.


typeset -a csv=()
csv=("${(@s/,/)row}")
[[ ${csv[1]}:e ]] || {echo "No file extension in column 1 of CSV… exiting."; exit 1 }
old_file="${csv[1]:gs/\"//}"
old_ext=${old_file:e}
new_file="${csv[2]:gs/\"//}.${old_ext}"



Mar 27, 2024 6:15 AM in response to VikingOSX

You should consider using extensions with filenames in both columns of a CSV. Otherwise, you would need to borrow the extension from the first column filename and append that extension to the newfile name. If no extension on first column filenames, then the script should quit.


typeset -a csv=()
csv=("${(@s/,/)row}")
[[ ${csv[1]}:e ]] || {echo "No file extension in column 1 of CSV… exiting."; exit 1 }
old_file="${csv[1]:gs/\"//}"
old_ext=${old_file:e}
new_file="${csv[2]:gs/\"//}.${old_ext}"



You are on a UNIX box and there should be one "." denoting the separator for the extension. Other dots should be replaced with "_".


Duplicates due to some very messed up hosting software… ;-)

Jul 21, 2023 5:39 AM in response to VikingOSX

Hi, Thank you so much for your time and your help.

It still doesn't work... It says that files has been renamed but the files are still the same.

  1. I have a doubt about something you said in the other post, "Replace the boiler plate for loop with the code from the Zsh code content below."

As a french, i don't understand where to paste the Zsh code; google translate do not understand this part ;)

So i try to replace everything in the shell script, and also to place your script between :

for f in "$@"
do

AND

	echo "$f"
done

I don't understand where the problem comes from... So sorry to bother you.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

20,000 files to rename - Automator / CSV

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