Author Topic: How to specify directory level  (Read 1326 times)

WP

  • Newbie
  • *
  • Posts: 7
How to specify directory level
« on: October 22, 2018, 06:14:29 PM »
Hi guys, can you help me out with this?

I would like to add the parent's folder parent name into a tag

Disk
>Photos 2018-10-23
>>RAW
>>JPG

From inside "RAW" I would like to add "Photos 2018-10-23" into title for instance.
How can I get this data?

This is the code I'm using (trying to..) now:
D:\Installers\EXIF\exiftool.exe -r -m -overwrite_original "-ImageDescription<Photos of ${Directory;s/^\.$/use Cwd; cwd();/e;s/.*\///;} by me" -api "Filter=$_=uc" "N:\NEW PHOTOS\2018-10-23" -ext ARW

Thanks a lot in advance!

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 3803
Re: How to specify directory level
« Reply #1 on: October 22, 2018, 07:02:26 PM »
Try this as a baseline
exiftool "-ImageDescription<Photos of ${Directory;$_=(split '\/',$_)[-2]} by me"
to make sure it's the directory you want, then add the rest of your command.

Split with split the directory into an array on the slash.  Then the number in the brackets indicates the element of the array, in this case, second from the last.

So N:\NEW PHOTOS\2018-10-23\RAW will get split into
N:
NEW PHOTOS
2018-10-23
RAW
(exiftool uses slashes / instead of backslashes \ for directory paths, even on Windows) and would return second from the last, in this case 2018-10-23

Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

WP

  • Newbie
  • *
  • Posts: 7
Re: How to specify directory level
« Reply #2 on: October 23, 2018, 04:50:50 AM »
Thanks al lot for getting me started!

Unfortunatly the result is not what we expected, I've used this command:
C:/EXIF/exiftool.exe -m -overwrite_original "-ImageDescription<Photos of ${Directory;$_=(split '\/',$_)[-2]} by me" RAW/ -ext nef

The directory is: C:\Users\WP\Desktop\TEST\2018-10-23\RAW

After modifying the command to:
C:/EXIF/exiftool.exe -m -overwrite_original "-ImageDescription<Photos of ${Directory;$_=(split '\',$_)[-2]} by me" RAW/ -ext nef

Now the description is written with the right foldername but I get a warning:
Warning: Can't find string terminator "'" anywhere before EOF for 'Directory' - RAW/0002.nef

Of course I can ignore these but it doesn't feel right :-)

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16724
    • ExifTool Home Page
Re: How to specify directory level
« Reply #3 on: October 23, 2018, 07:23:09 AM »
Odd.  StarGeek's command looks right to me, but maybe the quotes or backslash are the problem.  Try this:

C:/EXIF/exiftool.exe -m -overwrite_original "-ImageDescription<Photos of ${Directory;$_=(split m(/),$_)[-2]} by me" RAW/ -ext nef

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 3803
Re: How to specify directory level
« Reply #4 on: October 23, 2018, 04:02:00 PM »
Unfortunatly the result is not what we expected, I've used this command:
C:/EXIF/exiftool.exe -m -overwrite_original "-ImageDescription<Photos of ${Directory;$_=(split '\/',$_)[-2]} by me" RAW/ -ext nef

The directory is: C:\Users\WP\Desktop\TEST\2018-10-23\RAW

The problem is that you appear to be starting in C:\Users\WP\Desktop\TEST\2018-10-23 and processing the RAW directory below it.  In that case, the Directory tag by itself is going to return ./RAW and it won't be split the way you want it.  You can either use the full path in the command with the Directory tag as you did in your first example, or you can use the FilePath tag, which will always return the complete path and will get properly split.

Quote
After modifying the command to:
C:/EXIF/exiftool.exe -m -overwrite_original "-ImageDescription<Photos of ${Directory;$_=(split '\',$_)[-2]} by me" RAW/ -ext nef

Now the description is written with the right foldername but I get a warning:
Warning: Can't find string terminator "'" anywhere before EOF for 'Directory' - RAW/0002.nef

By removing the slash /, you're now escaping the single quote which is why there's a problem with the string termination.  Internally, exiftool will treat all directory paths as having a slash / directory separator, even on Windows.  So you need to escape the slash in the split command with a backslash.

I would suggest not using the second command going forward, as you may introduce errors down the line.  Switch Directory to FilePath should provide the correct results.
C:/EXIF/exiftool.exe -m -overwrite_original "-ImageDescription<Photos of ${FilePath;$_=(split m(/),$_)[-2]} by me" RAW/ -ext nef
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

WP

  • Newbie
  • *
  • Posts: 7
Re: How to specify directory level
« Reply #5 on: October 23, 2018, 05:09:39 PM »
Thanks!  ;D I got the result I wanted and added some more fields:

C:/EXIF/exiftool.exe -m -overwrite_original -all= "-Title<${FilePath;$_=(split m(/),$_)[-3]}" "-ImageDescription<PHOTOS OF ${FilePath;$_=(split m(/),$_)[-3]} BY ME" "-Writer-Editor=ME" "-SubjectReference<${filename;s/\.[^.]*$//}" -rights="ME, ALL RIGHTS RESERVED" "-by-line=ME" "-credit=ME" "-CopyrightNotice=ALL RIGHTS RESERVED BY ME" "-source=ME.COM" -api "Filter=$_=uc" RAW/ -ext *

WP

  • Newbie
  • *
  • Posts: 7
Re: How to specify directory level
« Reply #6 on: June 03, 2020, 04:45:50 AM »
I am sorry for waking up this topic but I saw no other way...

The metadata rewriting is working perfectly and now I'm facing the challenge of renaming the files, preferably with the same script to keep things simple (or complicated ;-) ).

At this moment I'm using an external tool to rename the photos but I would prefer to use only Exiftool to no longer be dependent on other software.

I have tested with C:/EXIF/exiftool.exe -m -overwrite_original -all= "-filename<${FilePath;$_=(split m(/),$_)[-3]}.arw" -ext ARW . -r

The images should be first ordered to date-original (because the filename/counter can reset to 0000 sometimes).
The filename should become: Photos 2018-10-23 #### RND5 / Photos 2018-10-23 0123 SKIFL

Unfortunately the filename is not changing, this is where I'm stuck.
Can you help me out, of course this will be very much appreciated! :-)

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16724
    • ExifTool Home Page
Re: How to specify directory level
« Reply #7 on: June 03, 2020, 05:56:15 AM »
The filename should become: Photos 2018-10-23 #### RND5 / Photos 2018-10-23 0123 SKIFL

I don't understand.  Can you give an example of what file name you want given the original FilePath of the file?

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

WP

  • Newbie
  • *
  • Posts: 7
Re: How to specify directory level
« Reply #8 on: June 04, 2020, 03:46:20 AM »
I'm sorry for being unclear.
I would like to achieve:

File structure before
Disk
>Photos 2018-10-23
>>Capture (raw)
>>>DCIM_001.arw
>>>DCIM_002.arw
>>>DCIM_003.arw
>>Output (jpg)

File structure after
Disk
>Photos 2018-10-23
>>Capture (raw)
>>>Photos_2018-10-23_0001_JKLSR.arw
>>>Photos_2018-10-23_0002_DFUYF.arw
>>>Photos_2018-10-23_0003_POKNH.arw
>>Output (jpg)

grandparent foldername
counter
random five char

Metadata after renaming: Title / ImageDescription / SubjectReference = "PHOTOS OF [filename without extension and underscores] BY ME"
« Last Edit: June 04, 2020, 11:07:30 AM by WP »

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 3803
Re: How to specify directory level
« Reply #9 on: June 04, 2020, 11:12:34 AM »
Messy.  Here's my attempt.  Replace TestName with Filename if it works correctly for you.
exiftool "-TestName<${FilePath;$_=(split m(/),$_)[-2]} %1.4C ${Filename;my $x='';$x.=[0..9,'A'..'Z']->[rand 16]for 1..5;$_=$x}.arw" /path/to/files/

Phil might be able to come up with something better for the random string, I just copy/pasted something I found on StackOverflow.

Also, you should not use -All on a RAW file type such as an ARW.  Doing so will quite probably render the image unviewable.

Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

WP

  • Newbie
  • *
  • Posts: 7
Re: How to specify directory level
« Reply #10 on: June 04, 2020, 11:57:37 AM »
Thanks a lot, we're very close!  :D

i changed it a bit to:

working from capture
exiftool "-Filename<${FilePath;$_=(split m(/),$_)[-3]} %1.4C ${Filename;my $x='';$x.=[0..9,'A'..'Z']->[rand 16]for 1..5;$_=$x}.arw" ../capture

working from project
exiftool "-Filename<${FilePath;$_=(split m(/),$_)[-3]} %1.4C ${Filename;my $x='';$x.=[0..9,'A'..'Z']->[rand 16]for 1..5;$_=$x}.arw" capture

The counter is not starting
Photos 2018-10-23 .4C 6DC72.arw
Photos 2018-10-23 .4C C1844.arw
Photos 2018-10-23 .4C C5412.arw

If I start a command prompt in the capture folder and use the code below it is working perfectly:

C:/APPS/exiftool.exe "-Filename<${FilePath;$_=(split m(/),$_)[-3]} %1.4C ${Filename;my $x='';$x.=[0..9,'A'..'Z']->[rand 16]for 1..5;$_=$x}.arw" ../capture

Result:
Photos 2018-10-23 0002 6251A.arw
Photos 2018-10-23 0001 AE944.arw > wrong order?
Photos 2018-10-23 0003 57BA1.arw

With photos from different minutes the counter is staying okay

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 3803
Re: How to specify directory level
« Reply #11 on: June 04, 2020, 12:18:08 PM »
working from capture

working from project

Not sure what you mean by that.  Batch file?  Then FAQ #27.  Otherwise if it's some other program, you'll need to see how they handle percent signs.  Because it looks like the % sign is getting dropped from the command.

Quote
With photos from different minutes the counter is staying okay

You might have to add the -FileOrder option.
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

WP

  • Newbie
  • *
  • Posts: 7
Re: How to specify directory level
« Reply #12 on: June 06, 2020, 05:14:52 AM »
You were right both times! I got the script right now :-)

I replaced the spaces in between the elements with _ to make the names www proof and changed the length of the ID to 8 char.

exiftool.exe -fileOrder DateTimeOriginal "-Filename<${FilePath;$_=(split m(/),$_)[-3];s/ /_/g}_%%1.4C_${Filename;my $x='';$x.=[0..9,'A'..'Z']->[rand 16]for 1..8;$_=$x}.arw" -api "Filter=$_=uc" -ext ARW capture

Thanks a lot for helping me out again, have a nice day!