Renaming a collection of files inside nested folders

This resource will permit you to very quickly rename any number of files inside a nested folder structure, and will add the names of the folders as prefixes in the filenames.

This is how the files would look after this is completed, and note that the names of both folders are included in the filenames of the images:

To accomplish this, we will create a 'Quick Action' in the Mac application 'Automator'. Once this Quick Action is established, it will exist as a right/control-clickable operation in Finder, so going forward we will not need to open Automator to perform this action.

Here is the folder structure that we will use for this resource, and note that the files have not been renamed yet in any highly consistent manner:

To start, open 'Automator'. Once open, click 'File' and then 'New'.

When a dialogue box appears, instead of 'Workflow', select 'Quick Action', and then 'Choose'.

After clicking 'Quick Action', another dialogue box will appear that will need to be customized a bit. In the dropdown that says 'Workflow receives current', select 'files or folders', and in the 'in' dropdown select 'Finder'.

Now scroll down the list of available 'Actions' on the left part of the application window and double-click 'Run Shell Script'.

Inside the 'Shell' drop-down menu of the 'Run Shell Script' dialogue box, select '/bin/zsh' if your operating system is on Catalina, Big Sur (or newer). If you are on an older operating system, select '/bin/bash'.

Since the computer for this demonstration is on Catalina, we are going to select '/bin/zsh'.

Now we have to insert some code inside the 'Run Shell Script' window for this Quick Action to work:

Paste the following code into the 'Run Shell Script' window.

for d in "$@"
    cd $d
    for f in *;
      cd $d/$f
      for e in *;
          NEWNAME="$d/$f/$(basename $d)_$(basename $f)_00$((n++)).$EXTENSION"
          echo "$OLDNAME ---> $NEWNAME" >> renameInfo.txt
          mv -- $OLDNAME $NEWNAME

That will look like this in Automator:

Now we are ready to give this Quick Action a try.

Make sure to save your Quick Action by selecting 'File', 'Save' and then rename your Quick Action something that you will remember (e.g., 'Rename', 'BatchRename', etc.).

Now navigate over to the folder that you want to rename. Right-click (or control-click) the folder, scroll down to 'Services', and find the item that matches your saved Quick Action. Select that Service.

The service operation will run very quickly, and once completed you should see that your files have been renamed in alignment with the nested folder structure.

Here is how that looks once completed:

In addition, you will see inside each child folder a 'renameinfo.txt' file that tells you what the original filenames "were," as well as what they are renamed "to" once the Quick Action is performed.

Good luck in your renaming adventures, and stay organized out there.