Rename files with multiple levels of parent directory

Started by Devereaux, May 17, 2021, 12:33:23 PM

Previous topic - Next topic

Devereaux

Hello,
I'm trying to append all parent directories starting from level 2 to their corresponding content files name. So for example:

CURRENT DIRECTORY
LVL1DIR1
>> LVL2DIR1
>>>> LVL3DIR1
>>>>>> file1.jpg
>>>>>> File2.jpg
>> LVL2DIR2
>>>> LVL3DIR1
>>>>>> LVL3DIR1
>>>>>>>> file3.jpg
>>>>>>>> file4.jpg

DESIRED OUTCOME
LVL1DIR1
>> LVL2DIR1
>>>> LVL3DIR1
>>>>>> file1_LVL2DIR1_LVL3DIR1.jpg
>>>>>> File2_LVL2DIR1_LVL3DIR1.jpg
>> LVL2DIR2
>>>> LVL3DIR1
>>>>>> LVL4DIR1
>>>>>>>> file3_LVL2DIR2_LVL3DIR1_LVL4DIR1.jpg
>>>>>>>> file4_LVL2DIR2_LVL3DIR1_LVL4DIR1.jpg

Please help!



Luuk2005

Greetings, its hard to understand but Im thinking you want to ignore "Level1" below the current directory ????   So please to first experiment with...

exiftool -ext jpg -fast4 -r -if "$Directory=~/(.*\/){2}/" -TESTName"<${Filename;s/\.[^.]*$//;}${Directory; s|./[^/]*||; s|/|_|g}${Filename;s/.*(\..*)/$1/}"  "FolderPath or ."

If this presents the new .jpg names like you are wishing for, then it can be safe changing -TESTName into -Filename.
Windows8.1-64bit,  exiftool-v12.92(standalone),  sed-v4.0.7

Devereaux

Thanks for your reply!
I tried your solution with a directory like this:
C:\IMAGES\MEL\MOUNTAIN\IMAGE1.JPG

Desired outcome is:
C:\IMAGES\MEL\MOUNTAIN\IMAGE1_MEL_MOUNTAIN.JPG

What your code output in my attempt:
C:\IMAGES\MEL\MOUNTAIN\IMAGE1C_IMAGES_MEL_MOUNTAIN.JPG

Please help!

Luuk2005

Many apologies, the command only works with "." for current directory, but not for specifying "C:/FolderPaths".
So you would have to cd up one-folder above images, to append all folders after images into the filename.
Windows8.1-64bit,  exiftool-v12.92(standalone),  sed-v4.0.7

Devereaux


Luuk2005

I should probably give better explanations for how this conducts...
exiftool -ext jpg -fast4 -r -if "$Directory=~/(.*\/){2}/" -TESTName"<${Filename;s/\.[^.]*$//;}${Directory; s|./[^/]*||; s|/|_|g}${Filename;s/.*(\..*)/$1/}"  "."

When using -r with "." for "current directory", $Directory always look like either...
.               -or-
./Level1    -or-
./Level1/Level2/Level3/Level4/etc...

So -if "$Directory=~/(.*\/){2}/" will only conduct folders at Level2 or below (when $Directory has at least two /'s).
Then to create a suffix, $Directory is modified by removing ./Level1 (./AnythingThatsNot/); then replacing all '/' with '_'.
So $Directory  ----------------------------> ${Directory; s|./[^/]*||; s|/|_|g}
./Level1/Level2/Level3/Level4 ------> _Level2_Level3_Level4

So then -TESTName"<${Filename;s/\.[^.]*$//}${Directory; s|./[^/]*||; s|/|_|g}${Filename;s/.*(\..*)/$1/}"
makes TESTnames like NameWithout.extension_Level2_Level3_Level4.extension

With new version, there is ${BaseName} instead of how I get NameWithoutExtension, and also probably something for .extension ?
But Im afraid to update, because not wanting the newer regex version to destroy some of my expressions.
Windows8.1-64bit,  exiftool-v12.92(standalone),  sed-v4.0.7

Devereaux

I actually did try to understand your code, but your explanation makes it way easier to comprehend

Thank you for taking your time explain it to me!