I have tested an Automator Quick Action, where I selected ten of what would be your parent folders in the Finder (using ⌘+A), each with five sub-folders, and each sub-folder containing a mixture of three PDF and DOCX files. The result was as expected with the parent folder prefixing the individual filenames (e.g. Mediafolder_filename.ext) following a rename operation.
I have added intelligence to the script so that multiple runs will not affect already renamed filenames within a given parent folder.
I also added an AppleScript display dialog to report: 1) parent folder count, 2) processed file count, and the elapsed time when the script has completed. Looks like this. If no files were renamed, then no dialog appears.
The BSD date command cannot resolve time less than a second and it took .174 ms to process everything in those ten parent folders, so no seconds appeared in that dialog for my test set.
So you launch /Applications/Automator, choose New Document, select Quick Action, and then click the Choose button. Then You configure the top of that Quick Action like this:
Next, you drag/drop a Utilities Library > Run Shell Script action right-ward into the large workflow window, and configure its header like this:
and in the default code it provides, select and remove everything so that you have a blank action. Now, copy/paste the following code into that Run Shell Script window:
#!/bin/zsh
: <<'COMMENT'
Rename every pdf|docx files in the selected parent folder hierarchy to embed the
parent folder name as a prefix to the individual document name.
Usage: Select one or more folders in the Finder for the QA to process
Tested: Zsh 5.8 on macOS 11.6
VikingOSX, 2021-10-18, Apple Support Communities, No warranties at all.
COMMENT
typeset -gi stime=0 etime=0 elapsed=0 parcnt=0 fcnt=0
function finished () {
osascript <<-AS
use scripting additions
display dialog "Parent folders: $1" & return & "Files: $2" & return & "Time: $3" with title "Processing Complete"
return
AS
return
}
#start time
stime=$(date -j -u +%s)
# process all selected parents
for parent in "${@}";
do
(( parcnt++ ))
# path to every pdf and docx in the parent's subfolders
for sub in ${parent}/**/*.(pdf|docx)(.N);
do
# skip files that already have parent folder prefix
[[ "${sub:a:r:t}" =~ "^(${parent:t})_.*" ]] && continue
# print "${sub:a:h:r}/${parent:t}_${sub:t}"
# rename to subfolder/basename_parentfolder.ext
mv "${sub}" "${sub:a:h:r}/${parent:t}_${sub:t}"
(( fcnt++ ))
done
done
# end time
etime=$(date -j -u +%s)
elapsed=$(( etime - stime ))
# only show dialog when files have been processed
(( fcnt )) && $(finished ${parcnt} ${fcnt} "$(date -j -u -f "%s" "${elapsed}" +%T)")
exit 0
Now, save the Quick Action to some practical name (e.g. Process Parent Folders).
I have a folder on my Desktop named XDIR. Using the Finder, I open that container folder in it are the ten parent test folders. In the Finder, I press ⌘+A to select all parent folders, and then on the first one, I right-click and choose Quick Actions > Process Parent Folders. The preceding completion dialog will appear when it is done renaming the files.