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

After testing the previous solution by the great VikingOSX here: [AUTOMATOR] - Rename Files in SubFolders … - Apple Community

Only the version 2021-10-18 works but the file names unfortunately are set in the opposite order desired.


Does someone know how to invert the order?


Like: Advertiser_Language_CodeName_LifeStyle_Video_filename


Here is the working code.


#!/bin/zsh

: <<'COMMENT'
Rename every jpg|png|gif|mp4|zip 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 doc with the extensions below in the parent's subfolders
    for sub in ${parent}/**/*.(jpg|png|gif|mp4|zip)(.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


Thanks,


MacBook Pro 17″, OS X 10.11

Posted on Sep 7, 2023 7:57 AM

Reply

Similar questions

8 replies

Sep 15, 2023 3:35 AM in response to Dudusx9

The script that I wrote does not dynamically handle folder depths below the parent directory. You originally indicated a depth of 5 and that is what I hard-coded in the script to produce the renamed filename segments. I unsuccessfully spent time attempting to make it folder hierarchy dynamic and could not get the Zsh shell to take a pattern where it was needed to achieve that goal.


I am never comfortable with hard-coding script syntax if there is a way to avoid it. I may need to review the current approach to see if there is another way to make this handle arbitrary folder depth and construct filenames per se.


The operating system sets the maximum filename size to 255 characters and the entire PATH to 1024 characters. You can confirm this from the Terminal:


getconf NAME_MAX /
getconf PATH_MAX /


At some dynamic folder hierarchy depth, you may exceed these system-enforced limits.

Sep 8, 2023 8:31 AM in response to Dudusx9

Apropos of nothing, you’re incrementally creating a database of videos, using the file system and shell scripts.


Maybe re-think the overarching design here, and also what you might want to add to this app in the future.


Particularly around data storage and access. i’d look to use FileMaker Pro or some other database to help organize all this data. Or to an existing content-storage system. And to EXIF or ilk, as dependencies on file system metadata too often end with grief and annoyance.


One other area that arises in pretty much all of these multi-media storage app designs involves confusion and consternation around dates and times, particularly around disagreements and conflicts between the file system metadata (use of that should be avoided) and the embedded EXIF or such metadata (use of that should be encouraged). For some background on this, see Phil Harvey’s ExifTool package.


Why? The app development path you’re on tends to become fragile and difficult to maintain and extend and scale.

Sep 15, 2023 2:48 AM in response to MrHoffman

I concede, leveraging meta data is useful when you deal with specific files and a system able to read, interpret and organise them. Thanks for sharing Phil Harvey's tool! The thing is that when you have a mix of file type like here (archive, flat image, vector images, video, audio, etc...) managed by people with different skills, the best approach to organise projects and files remains at folder level.

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 in order

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