Using "if" condition for renaming and executing other commands

Started by Jan, March 11, 2022, 07:27:40 AM

Previous topic - Next topic

Jan

ExifTool 12.39, commands run as bat file on Windows 10

Hello everybody,
First of all - big thanks to Phil for excellent tool!!

Would like to ask for help and advice with following - I am trying to make rather complex script which would:
- select only files in the folder with certain string in the name (DSC);
- preserve the original file name;
- rename the file using CreateDate in the form YYMMDD_seq (220102_001)
- add copyright, creator website and change status to "copyrighted"

While I am able to do all of that using separate scripts I can't find a way how to execute all commands in one single script.

Issues:
1) Can't find a way how to use single condition only in the first part of the script and then execute subsequent commands on that given file which is already "loaded" or processed.
Not sure here if the only way is to repeat that the same if condition before every single executed part.

2) When the renaming part is at the end of the script then there is "error erasing original file" which of course doesn't exist anymore as it was renamed and as a result the commands are not executed at all.

3) But while the renaming part is at the start of the whole script then the file is renamed and the other parts of the script are not executed as the file name fail the "if" condition because it does not contain that string anymore...


One way to achieve what I want would be to add those metadata first using the if condition and then run separate script to rename those files but having them all in one script would be preferred option.

I would also be grateful if someone could check that the script is fine and that there are no redundant commands.

Thank you very much



exiftool -if "${FileName =~ /DSC/" -P -overwrite_original "-XMP:PreservedFileName<Filename" "-FileName<CreateDate" -d "%%y%%m%%d_%%%%.3nc_AB.%%%%e" -execute -if "${FileName;$_=substr($_,0,4)} eq '_DSC'" -P -overwrite_original -tagsFromFile @ "-EXIF:DateTimeOriginal<FILE:FileCreateDate" -DateTimeOriginal -IPTC:DateCreated -IPTC:TimeCreated -EXIF:ModifyDate -EXIF:Copyright -IPTC:CopyrightNotice -XMP:Rights -XMP:WebStatement="www.website.com" -XMP:CreatorWorkURL="www.website.com" -XMP-dc:Creator="Name Surname" -XMP-xmprights:marked=true -execute -if "${FileName;$_=substr($_,0,4)} eq '_DSC'" -P -overwrite_original -tagsfromfile %%d%%f.%%e -E -d %%Y "-XMP:rights<&copy; $DateTimeOriginal Name Surname, all rights reserved" -common_args -ext NEF

Phil Harvey

Why aren't you doing all this in a single command?

If you need separate commands, you can use the $OK variable in your -if condition to see if the previous -execute'd command was successful.

- 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 ($).

Jan

Hi Phil
Thank you for your reply and sorry if my post was not clear - the thing is that I want to run it as a single command but due to mentioned issues it doesn't work.

Jan

Phil Harvey

Hi Jan,

I'm still not getting it.  Breaking down your command:

exiftool -if "${FileName =~ /DSC/"

You have forgotten a closing brace here

-P -overwrite_original "-XMP:PreservedFileName<Filename" "-FileName<CreateDate" -d "%%y%%m%%d_%%%%.3nc_AB.%%%%e" -execute

-if "${FileName;$_=substr($_,0,4)} eq '_DSC'"

I don't get this.  Are you trying to select the same files as in the first command?  If so, why not just remove the previous -execute and this -if condition?

-P -overwrite_original -tagsFromFile @ "-EXIF:DateTimeOriginal<FILE:FileCreateDate"

-DateTimeOriginal -IPTC:DateCreated -IPTC:TimeCreated -EXIF:ModifyDate -EXIF:Copyright -IPTC:CopyrightNotice

You're coping these tags into themselves.  Why?

-XMP:Rights -XMP:WebStatement="www.website.com" -XMP:CreatorWorkURL="www.website.com" -XMP-dc:Creator="Name Surname" -XMP-xmprights:marked=true -execute

-if "${FileName;$_=substr($_,0,4)} eq '_DSC'"

You're processing same files again.

-P -overwrite_original -tagsfromfile %%d%%f.%%e

And copying tags within the same file (you could use @ instead of %%d%%f.%%e).

-E -d %%Y "-XMP:rights<&copy; $DateTimeOriginal Name Surname, all rights reserved"

Unless the -E conflicts with your other commands, you should be able to do this in the previous command by doing this instead:

-E "-XMP:rights<&copy; ${filecreatedate#;DateFmt('%%y')} Name Surname, all rights reserved"

So my question is, why doesn't this single command do what you want?:

exiftool -if "${FileName} =~ /DSC/" -P -overwrite_original "-XMP:PreservedFileName<Filename" "-FileName<${CreateDate;DateFmt('%%y%%m%%d_%%%%.3nc_AB.%%%%e')}" "-EXIF:DateTimeOriginal<FILE:FileCreateDate" -XMP:WebStatement="www.website.com" -XMP:CreatorWorkURL="www.website.com" -XMP-dc:Creator="Name Surname" -XMP-xmprights:marked=true -E "-XMP:rights<&copy; ${filecreatedate#;DateFmt('%%y')} Name Surname, all rights reserved" -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 ($).

Jan

Hi Phil,
Thank you for your reply.

The point is to process the same file... but you right that there are many things wrong in my command.

The thing is this - there are "original" files in the directory containing that "DSC" string in their name plus there are renamed files (220102_001).
So my aim is to select and process only files containing "DSC" string and not renamed ones.
Later on I will add new files which I want to process and so on.

Means that all those commands I want to execute only on files which pass that "if" condition.

During my previous attempts it did not work without that -execute commands, not sure why.

Thank you
Jan

Jan

Hi Phil,
tested your script and it is doing exactly what I want, thank you very much!

Re to your questions why I did not do my script in other way - well apparently there is still lot for me to learn... and your reply definitely helped me a lot.

Added one more condition - CreateDate, hope it is correct this way:

exiftool -if "${FileName} =~ /DSC/ and ${CreateDate} =~ /2022:01:14/"


Would you also please help me with one more request related to my post - want to export list of original and renamed files in pairs (separated by tab) on separate lines, for each date (as by that CreateDate condition) in respective file.


DSC_0100 220114_001_AB
DSC_0101 220114_002_AB


Thank you very much again,
Jan


Jan

Hello Phil
I realized that my last question about output file was not any clear.

What I want to achieve is not a list of original and new names of all files in the directory but rather want to add a name pair (original and new name) after each file is processed by above discussed command ("if" condition, renaming, adding meta tags).

1) Is there any way to process the file, add its original and new name into output file, then process another file and add name pair to the same output file and so on?


Another question is about tags separator:

command
exiftool -f -csv >output.txt -sep "; " -PreservedFileName -FileName -ext NEF DIR
creates file with tags separated by commas and not by semicolons as stated by -sep

while this command works well

exiftool -f >output.txt -p "$PreservedFileName   $FileName" -ext NEF DIR


2) What is wrong about that -sep parameter?

Thank you very much,
Jan

Phil Harvey

Hi Jan,

Quote from: Jan on March 16, 2022, 10:01:47 AM
Hello Phil
I realized that my last question about output file was not any clear.

What I want to achieve is not a list of original and new names of all files in the directory but rather want to add a name pair (original and new name) after each file is processed by above discussed command ("if" condition, renaming, adding meta tags).

1) Is there any way to process the file, add its original and new name into output file, then process another file and add name pair to the same output file and so on?

Currently the only way is to use the -v option and search for lines like 'INFILE' --> 'OUTFILE'

Quotecommand
exiftool -f -csv >output.txt -sep "; " -PreservedFileName -FileName -ext NEF DIR
creates file with tags separated by commas and not by semicolons as stated by -sep

The -sep option doesn't apply to the -csv formatting.  CSV is always comma separated.

- 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 ($).

Jan


chuck lee

Hi Jan,

From the exiftool manual, when you write something to a tag, no read operation is allowed.  Therefore, in order to display the original file name and the new name meets the if condition, the -p option for a dry_run can help.  After you confirm the output is what you want, then issue the command to make the change.  For example, if you have file names like:
IMG_0005.jpg
IMG_0008.jpg


exiftool  -p '${filename;s/(.*)/$1\t/}${datetimeoriginal;DateFmt("%Y%m%d")}${filename;s/IMG//}' IMG*.jpg > out.txt
"\t" is for Tab
out.txt:
IMG_0005.jpg   20210306_0005.jpg
IMG_0008.jpg   20210306_0008.jpg


If the output is what you want, issue the command to change it.
exiftool  '-filename<${filename;s/(.*)/$1\t/}${datetimeoriginal;DateFmt("%Y%m%d")}${filename;s/IMG//}' IMG*.jpg
Just an example, you need to add the if condition as you want.



Jan

Hi Chuck,
sorry for such a late reply and thank you for your comments

Jan

Hello Phil
I am back to my original topic.
Your solution works great in a way that it adds copyright info, rename the file and keeps the original file name.

However I found out that it also replaces "date shot" + "date created" by the time and date I run the script which is unwanted result.


Would you please help me to fix this in the script you suggested?

Thank you very much,
Jan


exiftool -if "${FileName} =~ /DSC/" -P -overwrite_original "-XMP:PreservedFileName<Filename" "-FileName<${CreateDate;DateFmt('%%y%%m%%d_%%%%.3nc_AB.%%%%e')}" "-EXIF:DateTimeOriginal<FILE:FileCreateDate" -XMP:WebStatement="www.website.com" -XMP:CreatorWorkURL="www.website.com" -XMP-dc:Creator="Name Surname" -XMP-xmprights:marked=true -E "-XMP:rights<&copy; ${filecreatedate#;DateFmt('%%y')} Name Surname, all rights reserved" -ext NEF

StarGeek

Quote from: Jan on June 04, 2022, 02:01:42 AMHowever I found out that it also replaces "date shot" + "date created" by the time and date I run the script which is unwanted result.

I'm guessing that you mean these dates


Run this command to verify and compare againstand FileCreateDate and  FileModifyDate.
exiftool -time:all -G -a -s file.jpg

The command you're running includes the -P (-preserve) option.  This should keep the FileModifyDate the same.

Is the file on a local disk?  Or is it somewhere else like a network drive?
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Jan

The files are on external HDD.

This photo was taken on 14. 1. 2022, 6:53:41, which is now saved as "Date File Modified" or "Date Modified" depending on SW used.

Changed to following depending on SW (sorry do not know how to paste image here):
a)
Date Created: 13. 3. 2022, 9:48:21
Date File Created: 13. 3. 2022, 9:48:21
Date File Modified: 14. 1. 2022, 6:53:41

b)
Date Created: 13. 3. 2022 9:48:21
Date Modified: 14. 1. 2022 6:53:41
Date Shot: 13. 3. 2022 09:48:21.27


When I rename the files using Adobe Bridge the outcome is as follows, dates are correct:
a)
Date Created: 14. 1. 2022, 6:53:41
Date File Created: 13. 3. 2022, 15:48:05
Date File Modified: 13. 3. 2022, 17:39:04

b)
Date Created: 13. 3. 2022, 15:48:05
Date Modified: 13. 3. 2022, 17:39:04
Date Shot: 14. 1. 2022, 6:53:41


StarGeek

Use the command I listed in order to find out the exact tag name.  Other programs will pull the time stamps from multiple tags.  For example, Bridge might be pulling from EXIF:DateTimeOriginal, IPTC:DateCreated + IPTC:TimeCreated, or XMP:DateCreated, as the are all corresponding tags in the three main namespaces.

Just copy/paste the output from exiftool and use the Code Button to format the output.  Copy the full output from before and after running your commands.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype