You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Renaming files within a numbered folder with Automator?

Hi there,

I have a folder and within that folder I have several (sub) folders named

01-Folder

02-Folder

03-Folder and so on. Within these numbered folders there 2 but sometimes more files named

file.pdf

file.rtf

file.numbers and so on.

Is it possible to batch rename my files so that they match the number of their parent folder, please?

In other words in 01-Folder, the file be numbered 01-file.pdf, 01-file.rtf, 01-file.numbers etc.

Thanking the community in advance.

iMac (2017 – 2020)

Posted on Sep 19, 2024 7:39 AM

Reply
Question marked as Top-ranking reply

Posted on Sep 20, 2024 8:17 AM

The solution can be readily incorporated into Automator as either an application that you double-click to select the parent folder, or as a Quick Action, where you select that folder in the Finder, right-click, and choose Quick Actions. Since you are running Ventura, it can also be implemented as a macOS Shortcut Quick Action.


Here is an Automator application solution that gets saved to your Desktop and you double-click it to choose the parent folder containing the nn-folder hierarchy. I have only tested this with the folder structure that you see in the preceding images and only on Sequoia v15.0.


Launch Automator and choose New Document > on your Desktop > Application > Choose.


In the left column are Libraries and you only need two actions that you drag and drop into the larger workflow window:

  1. Library: Files and Folder
    1. Action: Ask for Finder Items
  2. Library: Utilities
    1. Action: Run Shell Script
    2. set the Shell: to /bin/zsh and the Pass input: as arguments
      1. Remove all content now in the Run Shell Script window


Copy and paste the following Shell script content into the now empty Run Shell Script window, and Save the Automator Application to your Desktop with a meaningful name.


#!/bin/zsh

: <<"COMMENT"
Provide parent folder name to this script on the command-line and it will
recursively descend into that folder hierarchy capturing the "nn-" prefix
of each so numbered folder and prepending that to the corresponding folder's
files.

Script can be run more than once if new files added to the folder hierarchy
and will not prepend folder numeric string to those files already processed.

Usage: 

Tested: macOS Sequoia v15.0, Zsh v5.9

COMMENT

# abort if no argument provided to script
(( $# )) || { echo "missing parent folder argument to script.";exit 1}

PARENT="${1:a:s/\~\//}"

# process in sort ascending (on) order excluding any "." dot files
for f in ${PARENT}/**/*(.Non);
do
    # check if file path has multiple occurrences of /nn- in it and skip the file
    # if this is the case
    count=$(/usr/bin/egrep -o "\/([0-9]+-)" <<<"${f}" | wc -l | tr -d '[ \n]')
    [[ $count -eq 1 ]] || continue

    # capture number string of folder name and trailing "-" or skip folder
    [[ "${f:r:h}" =~ ".*/([0-9]+-).*" ]] || continue
    # rename (but do not overwrite) files with prepended folder match string
    /bin/mv -n "${f}" "${f:r:h}/${match}${f:t}"
done
/usr/bin/osascript -e "display dialog \"Processing Done.\""
exit 0




7 replies
Question marked as Top-ranking reply

Sep 20, 2024 8:17 AM in response to petepompei

The solution can be readily incorporated into Automator as either an application that you double-click to select the parent folder, or as a Quick Action, where you select that folder in the Finder, right-click, and choose Quick Actions. Since you are running Ventura, it can also be implemented as a macOS Shortcut Quick Action.


Here is an Automator application solution that gets saved to your Desktop and you double-click it to choose the parent folder containing the nn-folder hierarchy. I have only tested this with the folder structure that you see in the preceding images and only on Sequoia v15.0.


Launch Automator and choose New Document > on your Desktop > Application > Choose.


In the left column are Libraries and you only need two actions that you drag and drop into the larger workflow window:

  1. Library: Files and Folder
    1. Action: Ask for Finder Items
  2. Library: Utilities
    1. Action: Run Shell Script
    2. set the Shell: to /bin/zsh and the Pass input: as arguments
      1. Remove all content now in the Run Shell Script window


Copy and paste the following Shell script content into the now empty Run Shell Script window, and Save the Automator Application to your Desktop with a meaningful name.


#!/bin/zsh

: <<"COMMENT"
Provide parent folder name to this script on the command-line and it will
recursively descend into that folder hierarchy capturing the "nn-" prefix
of each so numbered folder and prepending that to the corresponding folder's
files.

Script can be run more than once if new files added to the folder hierarchy
and will not prepend folder numeric string to those files already processed.

Usage: 

Tested: macOS Sequoia v15.0, Zsh v5.9

COMMENT

# abort if no argument provided to script
(( $# )) || { echo "missing parent folder argument to script.";exit 1}

PARENT="${1:a:s/\~\//}"

# process in sort ascending (on) order excluding any "." dot files
for f in ${PARENT}/**/*(.Non);
do
    # check if file path has multiple occurrences of /nn- in it and skip the file
    # if this is the case
    count=$(/usr/bin/egrep -o "\/([0-9]+-)" <<<"${f}" | wc -l | tr -d '[ \n]')
    [[ $count -eq 1 ]] || continue

    # capture number string of folder name and trailing "-" or skip folder
    [[ "${f:r:h}" =~ ".*/([0-9]+-).*" ]] || continue
    # rename (but do not overwrite) files with prepended folder match string
    /bin/mv -n "${f}" "${f:r:h}/${match}${f:t}"
done
/usr/bin/osascript -e "display dialog \"Processing Done.\""
exit 0




Oct 15, 2024 6:59 AM in response to petepompei

petepompei wrote:
Hi there,
@MartinR. Any chance you could walk me through the steps to achieve this with of A Better Finder Rename, please?

Using your example of 01-Folder containing file.pdf, file.rtf, file.numbers


In A Better Finder Rename (ABFR):

Drag the files you want to rename into the Rename panel

In the Actions panel, select these actions:

  • Parent Folder Name > Add folder to beginning of name
  • Character Ranges & Positions > Remove a range of characters
    • From text postion = 4 from start (the F in the word Folder)
    • To text position = 9 from start (the r in word Folder)


The Rename panel always shows you 1) the current file names, 2) the effect of the actions and 3) the renamed file names, so you always see how the files will be renamed before they are actually renamed. You only commit renaming by clicking the Perform Renames button. You then get a confirmation window showing the exact changes and a choice of Cancel, Rename one file or Rename all.


The results would be 01-file.pdf, 01-file.rtf, 01-file.numbers in the folder "01-Folder"


A Better Finder Rename has a free trial, so you can give it a try to see if it works for you.


Sep 21, 2024 5:32 AM in response to VikingOSX

And here is a link to install a Shortcut Quick Action that goes even further than the Automator solution and handles additional sub-folder hierarchy.


Parent

|_ 01-Folder

|_ 05-Folder

|_ nn-Folder


When run more than once, it only prepends the folder prefix to new files that were not processed previously. One may need to visit System Settings > 🔍 Finder and click on Login Items & Extensions. Scroll down to Finder and select it to enable the new Quick Action Prepend Finder Name.


Then, you can right-click on the Parent folder, and on the Finder secondary menu, select Quick Actions -> Prepend Folder Name.


Sep 20, 2024 12:33 PM in response to petepompei

petepompei wrote:
Is it possible to batch rename my files so that they match the number of their parent folder, please?
In other words in 01-Folder, the file be numbered 01-file.pdf, 01-file.rtf, 01-file.numbers etc.

You would need code in addition to Automator to do that, as @VikingOSX already indicated.


If you are not into using code, A Better Finder Rename can do what you want to do. I use it all the time to batch rename files, including applying the parent folder name to each file in a folder. In your case, to use only the first 3 characters of the parent folder name ("nn-") you would create a multi-step rename action in ABFR. First step to prepend the parent folder name, second step to remove the 4th & subseqent characters from the parent folder name, leaving "nn-filename" as the result.


Works like a charm.

Oct 15, 2024 5:35 AM in response to petepompei

Hi there,

Thank you for your replies. I am sorry that I haven't replied earlier but I was very ill and I couldn't get to a computer .@VikingOSX. I have followed and tried your method but I can't seem to make it work.

@MartinR. Any chance you could walk me through the steps to achieve this with of A Better Finder Rename, please?

Once again thank you both for your replies and sorry for the very late reply.

Renaming files within a numbered folder with Automator?

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