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:
- Library: Files and Folder
- Action: Ask for Finder Items
- Library: Utilities
- Action: Run Shell Script
- set the Shell: to /bin/zsh and the Pass input: as arguments
- 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