Including directory in file name

Started by carrmandie, October 07, 2019, 06:07:45 PM

Previous topic - Next topic

carrmandie

I'm renaming photos from a lot of cameras at once. Not all of the cameras have a unique userlabel tag, but they are all in unique folders. For example, three cameras may have the folder structures:
E:/OriginalPhotos/10A/123
E:/OriginalPhotos/10A/456
E:/OriginalPhotos/32A/101

I'd like to rename all photos in the parent directory E:/OriginalPhotos to include the last two sub-directories, so that the rename structure would be something like this:
E:/RenamedPhotos/10A_123_20191007_155700

If I were processing one camera at a time, I'd use this script, where I just write in the unique information manually:
exiftool -r -d %Y%m%d_%H%M%S "-filename<E:/RenamedPhotos/10A_123_${CreateDate}.%e" E:/OriginalPhotos/10A/123

I know that there is a Directory tag that I could call from within the filename argument, but I'm only interested in the last 2 sub-directories. Is there a way to extract those automatically for each photo? I'm running this script using R, so in theory I could write a loop function to go through, grab the relevant substring, and paste it in there, but loop functions are less than ideal when dealing with hundreds of thousands of photos.

StarGeek

Test it out first using TestName instead of Filename but try something like this
exiftool -r -d %Y%m%d_%H%M%S "-Filename<E:/RenamedPhotos/${Directory;$_=(split '\/',$_)[-2]}_${Directory;$_=(split '\/',$_)[-1]}_${CreateDate}.%e" E:/OriginalPhotos

The directory part splits the entire directory path into an array based upon the slashes (exiftool uses slash / internally, even on Windows).  It then takes the second to last [-2] and last [-1] parts of that array and sets that as the value for the tag.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

carrmandie

Perfect! That's exactly what I was looking for. Thank you!