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

Recursive Subfolder File Renaming using a CSV in Automator/Apple Script

Hello,


Total noob here. I'm trying to automate file renaming ideally in a hot folder, where i can drop a parent folder containing a two column .csv and subfolders and have the shell script parse through and rename everything.

I have tried copying various scripts and combos from different questions, but can only get it to rename files in parent folder. Can anyone help? i've been reading answers on here and on stack overflow all day and i can't make this work.


I copied this code from another answer but don't understand how to add recursion to it?



This is the folder structure i want to use:




And this is what the .csv will look like:


it seems like it should be so simple..and yet...i can't figure it out !


Thanks so much in advance!!

MacBook Pro 16″, macOS 11.5

Posted on Sep 8, 2021 12:54 PM

Reply
Question marked as Best reply

Posted on Sep 9, 2021 4:59 AM

Laura,


I have modified your original Automator application code and it works exactly like my Zsh solution (which I tossed). The one change is that the CSV needs to look like this (with filename.ext):



Replace all of the code in your Run Shell Script with this code:


#!/bin/bash

cd "$1"
# just find the CSV whereever its at
CSVFILE="$(find $PWD -iname "*.csv")"

# strip quoted CSV fields (if present) and replace CRLF (if present) with LF
# dynamically adapt IFS to different CSV delimiters
while IFS=$'\t,;:|' read oldfile newfile
do
    # per Camelot suggestion to recurse into folder hierarchy and rename there
    find $PWD -name "$oldfile" -execdir mv "$oldfile" "$newfile" \;
done < <(sed -e 's/"//g' < $CSVFILE | tr -d '\r')
exit 0


I have tested this separately on Mojave 10.14.6 and Big Sur 11.5.2 as an Automator Application. I copied my TestDir folder hierarchy onto the TaxonomyRenamer folder on the Desktop and that latter folder was selected by the Ask for Finder Items. Produces the same results as I posted earlier:


Similar questions

10 replies
Question marked as Best reply

Sep 9, 2021 4:59 AM in response to laura_gel

Laura,


I have modified your original Automator application code and it works exactly like my Zsh solution (which I tossed). The one change is that the CSV needs to look like this (with filename.ext):



Replace all of the code in your Run Shell Script with this code:


#!/bin/bash

cd "$1"
# just find the CSV whereever its at
CSVFILE="$(find $PWD -iname "*.csv")"

# strip quoted CSV fields (if present) and replace CRLF (if present) with LF
# dynamically adapt IFS to different CSV delimiters
while IFS=$'\t,;:|' read oldfile newfile
do
    # per Camelot suggestion to recurse into folder hierarchy and rename there
    find $PWD -name "$oldfile" -execdir mv "$oldfile" "$newfile" \;
done < <(sed -e 's/"//g' < $CSVFILE | tr -d '\r')
exit 0


I have tested this separately on Mojave 10.14.6 and Big Sur 11.5.2 as an Automator Application. I copied my TestDir folder hierarchy onto the TaxonomyRenamer folder on the Desktop and that latter folder was selected by the Ask for Finder Items. Produces the same results as I posted earlier:


Sep 8, 2021 5:08 PM in response to laura_gel

The bash shell that Apple ships with macOS is far too old to support recursion, but the zsh shell does, and that is what I have used in an Automator Folder Action with a Run Shell Script solution.


Given my test folder looks like this:



and the data.csv contents are:


After I drop the test folder on the DropFolder, I get this result:



Is this the type of result you are wanting to achieve? If so, I will post the Automator solution in a response here.

Sep 8, 2021 5:27 PM in response to laura_gel

It's not too hard to to switch, but you do need to understand some of the nuances of shell scripts.


At first pass, if you want to keep this within a shell script, I'd probably change the mv command to a find command with the -execdir switch to make the move in the subdirectories.


Replace everything between do and done with:


find $PWD -name "$oldfile" -execdir mv "$oldfile" "$newfile" \;


This command looks for files in any subdirectory of the nominated folder. For any matches it executes the mv command to rename the file.




Sep 15, 2021 10:22 AM in response to VikingOSX

I am beyond confused, i tried the automator script again just now while trying to find the <> you were talking about and it worked?? I tried it maybe 30 times over the last week, the only thing that’s changed on my end was a software update yesterday? I have no idea what changed but you solved my problem! I also love that you incorporated Camelot’s suggestion in the code. Thank you for checking in and again, sorry i left you hanging

Recursive Subfolder File Renaming using a CSV in Automator/Apple Script

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