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.

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

[AUTOMATOR] - Rename Files in SubFolders by adding "_(ParentFolderName)"

I have 550 ParentFolders, each containing a few SubFolders. Each SubFolder has a few .docx files and a few .pdf files.


My current scenario is like this:


I need to rename all my files inside the SubFolder by adding "_" + "(ParentFolderName)", like this:



Is there a way I can do a Quick Action on all my 550 ParentFolders at the same time and rename all my files (like seen above)?


Anyone can offer some help?


Thanks!


Daniel



Posted on Oct 17, 2021 12:44 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 18, 2021 12:08 PM

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.

12 replies
Question marked as Top-ranking reply

Oct 18, 2021 12:08 PM in response to DanielP4321

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.

Oct 17, 2021 4:22 PM in response to DanielP4321

Give Name Mangler a try. You could drag the Parent Folder into NM's window and use the Add Suffix option to add the Parent Folders name to all files in the Parent Folder. Parent folder = EM Images.



You would want to move the parent folder from the list an it would also get the sufficient added if left in.


Someone who has an excellent grasp of AppleScript could probably create a script to to multiple Parent folders at a time but that's way above my pay grade.




Oct 17, 2021 5:26 PM in response to VikingOSX

@vikingOSX, ideally I would select all the Folders ("MediaFiles" and "VideoFiles" together) and right-click to then activate the Quick Action, instead of right-clicking on the "TESTDIR". Because I might add more Folders in the future to "TESTDIR" and by running the script again it might generate additional text to the already renamed files. Does it make sense?

Oct 18, 2021 6:36 AM in response to DanielP4321

Right now, I have an Automator Quick Action that lets you select all of the parent folders in a master folder by using ⌘+A in that Finder window, and it will then process every parent and parent's subfolder, prefixing the .docx and .pdf files with the parent folder name. I have also enabled a test to check if a filename already has this parent folder prefix, and the script will skip the file if it does.


I have run this against five selected folders multiple times with the single renaming result in every parent's subfolder.


Thinking about adding a visual dialog to let you know when the Quick Action has finished.

[AUTOMATOR] - Rename Files in SubFolders by adding "_(ParentFolderName)"

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