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

This post was what I was looking for at the beginning.

[AUTOMATOR] - Rename Files in SubFolders … - Apple Community


Like the requester, I now want to do the same recursively, so performing the action at top parent folder (Brand1) would batch rename all subfiles into each subfolders following the folders paths.



Anyone can advice how to do it?


Thanks


Dudu


MacBook Pro

Posted on Dec 11, 2022 3:45 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 11, 2022 11:39 AM

I had to revise and test the script to exclude the parent directory name from the renamed filename, so output looks like it did previously:




This came with a time price as the Zsh script in the Terminal worked fine and Automator threw a wrench into the result. So fixed the Automator result per the above tree diagram.


Code:


#!/bin/zsh

: <<'COMMENT'
Rename every file in the selected parent folder hierarchy with the prefix
string of the sub-folder hierarchy to the file punctuated by underscores. The
parent directory name is removed from the path string comprising the new filename.
Multiple runs do not process previously renamed files in the parent folder hierarchy.

Reference: https://discussions.apple.com/thread/254456435

Example:

Job1/Brand2/Personae1/FR/Animated/300x250.zip => Brand2_Personae1_FR_Animated_300x250.zip

Usage: Used as a Quick Action, select one or more parent folders in the Finder.
Tested: Zsh 5.8.1 on macOS 13.0.1
VikingOSX, 2022-12-11, 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
}

function elapsed_time () {
    local secs=$1
    # output is 0d:0h:0m:0s format
    # https://unix.stackexchange.com/questions/27013/displaying-seconds-as-days-hours-mins-seconds
    echo $(($secs/86400))d:$(($(($secs - $secs/86400*86400))/3600))h:$(($(($secs - $secs/86400*86400))%3600/60))m:$(($(($secs - $secs/86400*86400))%60))s
    return
}

#start time
stime=$(date -j -u +%s)

# permit case-insensitive file and folder name treatment
setopt nocaseglob
for parent in "${@}";
do
    (( parcnt++ ))
    for subfolder in ${parent}/**/*(.N);
    do
        # skip files already beginning with subfolder and underscore in their names
        [[ "${subfolder:t}" =~ "^.*([_]+).*$" ]] && continue

        # get $HOME and parent folder
        trimP="${subfolder:h5}/"
        # remove $HOME and parent folder name from new filename using trimP pattern
        ppath="${subfolder#${~trimP}}"
        # change '/' to '_' in new filename
        fpath="${ppath:gs/\//_/}"

        mv "${subfolder:h:r}/${subfolder:t}" "${subfolder:h:r}/${fpath}"
        (( fcnt++ ))
    done
done

# end time
etime=$(date -j -u +%s)
elapsed=$(( etime - stime ))

# only show finished dialog when new files have been processed
(( fcnt )) && $(finished ${parcnt} ${fcnt} $(elapsed_time $elapsed))
exit 0




3 replies
Question marked as Top-ranking reply

Dec 11, 2022 11:39 AM in response to Dudusx9

I had to revise and test the script to exclude the parent directory name from the renamed filename, so output looks like it did previously:




This came with a time price as the Zsh script in the Terminal worked fine and Automator threw a wrench into the result. So fixed the Automator result per the above tree diagram.


Code:


#!/bin/zsh

: <<'COMMENT'
Rename every file in the selected parent folder hierarchy with the prefix
string of the sub-folder hierarchy to the file punctuated by underscores. The
parent directory name is removed from the path string comprising the new filename.
Multiple runs do not process previously renamed files in the parent folder hierarchy.

Reference: https://discussions.apple.com/thread/254456435

Example:

Job1/Brand2/Personae1/FR/Animated/300x250.zip => Brand2_Personae1_FR_Animated_300x250.zip

Usage: Used as a Quick Action, select one or more parent folders in the Finder.
Tested: Zsh 5.8.1 on macOS 13.0.1
VikingOSX, 2022-12-11, 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
}

function elapsed_time () {
    local secs=$1
    # output is 0d:0h:0m:0s format
    # https://unix.stackexchange.com/questions/27013/displaying-seconds-as-days-hours-mins-seconds
    echo $(($secs/86400))d:$(($(($secs - $secs/86400*86400))/3600))h:$(($(($secs - $secs/86400*86400))%3600/60))m:$(($(($secs - $secs/86400*86400))%60))s
    return
}

#start time
stime=$(date -j -u +%s)

# permit case-insensitive file and folder name treatment
setopt nocaseglob
for parent in "${@}";
do
    (( parcnt++ ))
    for subfolder in ${parent}/**/*(.N);
    do
        # skip files already beginning with subfolder and underscore in their names
        [[ "${subfolder:t}" =~ "^.*([_]+).*$" ]] && continue

        # get $HOME and parent folder
        trimP="${subfolder:h5}/"
        # remove $HOME and parent folder name from new filename using trimP pattern
        ppath="${subfolder#${~trimP}}"
        # change '/' to '_' in new filename
        fpath="${ppath:gs/\//_/}"

        mv "${subfolder:h:r}/${subfolder:t}" "${subfolder:h:r}/${fpath}"
        (( fcnt++ ))
    done
done

# end time
etime=$(date -j -u +%s)
elapsed=$(( etime - stime ))

# only show finished dialog when new files have been processed
(( fcnt )) && $(finished ${parcnt} ${fcnt} $(elapsed_time $elapsed))
exit 0




This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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

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