How to specify directory level

Started by WP, October 22, 2018, 06:14:29 PM

Previous topic - Next topic

WP

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

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

* 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).

WP

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

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/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

Quote from: WPNL on October 23, 2018, 04:50:50 AM
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.

QuoteAfter 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
* 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).

WP

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

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

Quote from: WP on June 03, 2020, 04:45:50 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/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

WP

#8
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"

StarGeek

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.

* 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).

WP

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

Quote from: WP on June 04, 2020, 11:57:37 AM
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.

QuoteWith photos from different minutes the counter is staying okay

You might have to add the -FileOrder option.
* 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).

WP

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!