Automate renaming file with its parent folder name

I have 100s of PDF files nested three folders deep that I need to rename with the parent folder's name. How can I automate this? All help appreciated!


Folder name A1

Folder name A2

Folder name A3

File name nnn >> rename this "Folder name A1"

Folder name B1

Folder name B2

Folder name B3

File name qqq >> rename this "Folder name B1"

Posted on May 31, 2023 9:59 AM

Reply
Question marked as Top-ranking reply

Posted on May 31, 2023 12:53 PM

Here may be one approach using a Zsh shell script. I have tested this on Ventura 13.4. Starting with a parent folder on my Desktop (e.g. Folder00), I have the following hierarchy:



and upon running the script from my Desktop via the Terminal:


cd ~/Desktop
chmod +x ./hier_rename.zsh
./hier_rename.zsh ./Folder00



the result becomes:


The Zsh script:


#!/bin/zsh

: <<"COMMENT"
Rename PDF located three-folder levels below provided PARENTDIR with the
topmost folder name under the PARENTDIR.

See Modifiers in command-line man zshexpn, or
https://zsh.sourceforge.io/Doc/Release/Expansion.html
COMMENT

PARENTDIR="${1:a:s/\~\//}"

setopt nocaseglob
# recursively drill looking for case-insensitive named PDF
for f in "${PARENTDIR}"/**/*.pdf(.N);
do
    # get name of topmost folder in the hierarchy below PARENTDIR
    # by splitting the located PDF path into an array and take the first
    # folder name as topfolder variable
    topfolder="${${(s:/:)f:a:h:r:t3}[1]}"
    # block overwrites with -n argument
    /bin/mv -n "${f:a}" "${f:a:h:r}/${topfolder}.${f:e}"
done
exit 0


Similar questions

5 replies
Question marked as Top-ranking reply

May 31, 2023 12:53 PM in response to SeanSebastian

Here may be one approach using a Zsh shell script. I have tested this on Ventura 13.4. Starting with a parent folder on my Desktop (e.g. Folder00), I have the following hierarchy:



and upon running the script from my Desktop via the Terminal:


cd ~/Desktop
chmod +x ./hier_rename.zsh
./hier_rename.zsh ./Folder00



the result becomes:


The Zsh script:


#!/bin/zsh

: <<"COMMENT"
Rename PDF located three-folder levels below provided PARENTDIR with the
topmost folder name under the PARENTDIR.

See Modifiers in command-line man zshexpn, or
https://zsh.sourceforge.io/Doc/Release/Expansion.html
COMMENT

PARENTDIR="${1:a:s/\~\//}"

setopt nocaseglob
# recursively drill looking for case-insensitive named PDF
for f in "${PARENTDIR}"/**/*.pdf(.N);
do
    # get name of topmost folder in the hierarchy below PARENTDIR
    # by splitting the located PDF path into an array and take the first
    # folder name as topfolder variable
    topfolder="${${(s:/:)f:a:h:r:t3}[1]}"
    # block overwrites with -n argument
    /bin/mv -n "${f:a}" "${f:a:h:r}/${topfolder}.${f:e}"
done
exit 0


May 31, 2023 10:54 AM in response to SeanSebastian

Simple - except for the details, and the devil is always in the details :)


First off, I'm guessing you don't actually want to:


> rename with the parent folder's name


Because you would end up with a file in the subdirectory called 'Folder name A1', whereas I suspect you really want it to be called 'Folder Name A1.pdf'. Yes, it's a detail. but it's kind of important.


Second is whether the folders are always three deep. Are there files in the intermediate folders that you need to process (or ignore?).


What about if there are multiple .pdfs in the third folder. They can't both be renamed to the top folder name. What should the script do then? Copy the first? add numbers to the subsequent files? ask the user? Again, another detail, but it's important to address.


Answering these (and, potentially, other gotchas I can't even think about yet) are prerequisites to getting a working script.

May 31, 2023 10:58 AM in response to SeanSebastian

Thanks, but that won't work as the parent folders are not sequentially named/numbered (sorry for implying so above). "Folder name A1" is unpredictable, so need to be copied to the file name, not built from a name sequencing structure. Pretty sure this needs (above my pay grade) automation like:

  • copy parent folder name
  • climb down folder tree until find file
  • rename file with copied folder name
  • move down to next parent folder and repeat

May 31, 2023 11:06 AM in response to Camelot

Detail devils, indeed! Thanks for digging into this.


1/ All folder and file names are unpredictable (i.e., not sequentially or otherwise pattern named)

2/ There is 1 parent folder above all (see below)

3/ I need "Folder Name A1.pdf"

4/ Folders are always 3 deep

5/ There are never files in intermediate folders

6/ There is only 1 file in each target folder


Folder name 00

Folder name A1

Folder name A2

Folder name A3

File name nnn.pdf >> rename this "Folder name A1.pdf"

Folder name B1

Folder name B2

Folder name B3

File name qqq.pdf >> rename this "Folder name B1.pdf"

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.

Automate renaming file with its parent folder name

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