Print output to console for testing

Started by gonnhirrim, May 17, 2021, 04:08:34 PM

Previous topic - Next topic

gonnhirrim

I'm trying to write a script to parse my photo collection and set the originaldatetime to the most accurate thing I have. That can mainly be: originaldatetime, modifydate or part of the directoryname. This requires several tests. And since I'm not yet very familiar with the syntax, most of what I'm trying yet fails. But I can't see why and I'm in need of some form of logging.
For example
exiftool -modifydate -if '$modifydate le ${directory;s/.*(\d{4}).*/$1/:12:31 23:59:59}' [i]file[/i]

This condition fails for my file. But I can't see why. It might be an error in the syntax. But that's not the question I have.

What I would want is to print the result of the if-condition. Or part of it, for example:
exiftool -print ${directory;s/.*(\d{4}).*/$1/ file   ... but that doesn't work.

Any advise?

Luuk2005

Greeting, the -print option is abbreviated to be -p instead, and the -p 'string' should also be quoted.
So more like... -if 'SomeCondition'  -p '${directory;s/.*(\d{4}).*/$1/}'     (using } to terminate the substitution)
Windows8.1-64bit, exiftool-v12.11(standalone), sed-v4.0.7

Luuk2005

What Im often do is something like....
exiftool -ext jpg -if '${Tag1;s///} condition ${Tag2;s///}'  -p '$Filename ---- ${Tag1;s///} ---- ${Tag2;s///}'  'FolderPath'

Then if Im getting troubles, changing -if to -if not, so then studying why the -if wont match my conditions.
But usually, Im starting without any -if, so then just using -p to look at all of my tag-substitutions first.
If its very many files, or complicated, you can write the filenames and tag-substitutions into a file with headers, for studying them later.
Windows8.1-64bit, exiftool-v12.11(standalone), sed-v4.0.7

StarGeek

The problem is that your tag is incorrectly structured.  Anything between the braces, after the tag name, must be legitimate Perl code.  What you did here was do a regex substitution with ":12:31 23:59:59" as the options, which doesn't work.

Because you're doing a comparison of one tag to the other, you want to complete the value you want to compare within the tag.
exiftool -modifydate -if '$modifydate le ${directory;m/(\d{4})/;$_=$1.":12:31 23:59:59"}' /path/to/files/
This matches and captures the year (\d{4}), then uses that capture $1 to create the a complete date using the concatenate operator (the dot), and assign all of that to the default variable $_, which is what holds the value of the tag.

You can use the -p (-printFormat) option as Luuk2005 shows to test what the value might be, but if the Perl code is incorrect in the first place, then there won't be any changes and you may get a syntax error warning.
* 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).

gonnhirrim

Thank you both for your quick reply.
I'm obviously in the beginning stages of learning here. I am aware that there are problems in my tag: that is also the reason I want to debug it  ;D. So my question was how best to debug with exiftool.
The -p option so far does what I was looking for (but was completely put on the wrong foot by the documentation, so didn't even consider it seriously). I wanted to see my expression being evaluated and this seems to work just fine:
exiftool -p '${directory;s/.*(\d{4}).*/$1/}' /path/to/files/
and
exiftool  -p '$modifydate le ${directory;s/.*(\d{4}).*/$1/}:12:31 23:59:59'  /path/to/files/
Which gives me
2014:01:05 20:34:57 le 2000:12:31 23:59:59
which is ideal for debugging.

What I didn't succeed in yet is to have that evaluated (to false, in the example) ....
exiftool  -p -if '$modifydate le ${directory;s/.*(\d{4}).*/$1/}:12:31 23:59:59'  /path/to/files/
Gives me:
Wildcards don't work in the directory specification
-if

StarGeek

Quote from: gonnhirrim on May 18, 2021, 09:48:42 AM
Which gives me
2014:01:05 20:34:57 le 2000:12:31 23:59:59
which is ideal for debugging.

Except it isn't.  It gives you the impression that exiftool will compare "2014:01:05 20:34:57" to "2000:12:31 23:59:59", when what actually is happening will be a syntax error, which exiftool will gracefully exit out of.  Here's an example of the situation as if it were actual Perl code
C:\>perl -e "my $modifydate='2014:01:05 20:34:57';my $directory='path/to/2000/files';print ($modifydate le ${directory;s/.*(\d{4}).*/$1/}:12:31 23:59:59) ? 'Success' :'Failure';"
Number found where operator expected at -e line 1, near "31 23"
(Missing operator before  23?)
syntax error at -e line 1, near "}:"
Execution of -e aborted due to compilation errors.


The ":12:31 23:59:59" either needs to be combined inside the tag or added to the $directory variable as a string.  This is just passing raw, unenclosed numbers and colons, which will always fail.

QuoteWhat I didn't succeed in yet is to have that evaluated (to false, in the example) ....
exiftool  -p -if '$modifydate le ${directory;s/.*(\d{4}).*/$1/}:12:31 23:59:59'  /path/to/files/
Gives me:
Wildcards don't work in the directory specification
-if

The -p (-printFormat) option requires a second argument.  In this case, you have the -if directly after the -p, so exiftool uses that as the format string for the -p option.  That causes the whole -if condition string to be treated as a file path to be processed.
* 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).

gonnhirrim

#6
Mm, this is proving harder than I'd thought. I'll have to dive in a bit deeper I guess. And apparently the way for debugging is using perl.
Also, what is the function of the semicolon ";" right after directory? It seems as if the substitute is applied to the variable, but my perl knowledge is far too limited :-\
I'm trying to fix the perl statement (using perl) you give, but this doesn't return anything:
my $modifydate='2014:01:05 20:34:57';
my $directory='path/to/2000/files';
print ($modifydate le ${directory;s/.*(\d{4}).*/$1/}) ? 'Success' :'Failure';

or even simpler:
print ${directory;s/.*(\d{4}).*/$1/};
no result, aargh

gonnhirrim

Okay, many of the issues I've been having are due to using Powershell :(

Luuk2005

#8
${Tag;} by itself auto-removes illegal characters...  ${Tag; PerlCode} lets you custom modify the $Tag.

So if your $Tag was originally "abcdefg"
Then ${Tag; s/a/x/; s/g/z} is  "xbcdefz"

Im not know anything about the perl, so cannot help with any perl commands, but with exiftool can do things like...
exiftool -if "$ModifyDate and ($ModifyDate le ${Directory; s/.*(\d{4}).*/$1/})" -p "$Filename -- |$ModifyDate| -- |${Directory; s/.*(\d{4}).*/$1/}|"  "."

It would only show 'success', and prints everything to present why it succeeds.
You can also reverse the output, with an -if "not Condition" to present why the -if does not succeed.
But I really never use -if, until after looking at my ${Tags; changes} first.
Windows8.1-64bit, exiftool-v12.11(standalone), sed-v4.0.7

StarGeek

Quote from: gonnhirrim on May 18, 2021, 12:10:47 PM
Also, what is the function of the semicolon ";" right after directory?

The semicolon is the end of line character for Perl.  When it's at the end of the tag name in braces, it's used to indicate the start of the Advanced formatting feature, which is basically embedding Perl code to affect the values of that tag.

QuoteI'm trying to fix the perl statement (using perl) you give, but this doesn't return anything:
my $modifydate='2014:01:05 20:34:57';
my $directory='path/to/2000/files';
print ($modifydate le ${directory;s/.*(\d{4}).*/$1/}) ? 'Success' :'Failure';

That was just an example to try and show you why the command wasn't working as you set it up.  It wasn't meant to actually be used in an exiftool command.

Quote from: gonnhirrim on May 18, 2021, 02:56:28 PM
Okay, many of the issues I've been having are due to using Powershell :(

With Powershell, you'll use single quotes to enclose parameters and double quotes inside to enclose strings.  Similar to Linux/Mac and opposite of CMD.  You also need to be aware that PS will corrupt binary data that is piped or redirected, which is important if you ever want to do something like extract thumbnail/preview images from an image.
* 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).

gonnhirrim

Thanks
I know I cannot execute perl code with exiftool. Of course I'm using perl.

The issues with Powershell are not only linux-quoting, I had that figured out. But it needs special escaping, as is explained here: https://adamtheautomator.com/exiftool/#Issues_in_PowerShell.

I really love the tool, but I don't really like the usability. I'm not planning to become an expert here, I just need to convert some stuff (once).

Luuk2005

The exiftool also executes perl, besides just inside of ${Tag;PerlCode}, but Im not expert to understand the perl, so I cannot advise.
The best I can do is give links, and maybe you can decipher, so this example uses a perl if-statement (inside of an exiftool -if) ...
https://exiftool.org/forum/index.php?&topic=11625.msg62438#msg62438

The exiftool options are stored inside of an args-file, and called like...  exiftool -@ 'Path/To/args-file' 'Path' end.jpg
With using an args-file, Im also thinking maybe then, you wont have all the troubles with powershell quoting ????
Also, if you wish to describe 'several tests' from the original post, Im certain the experts could better advise.
Windows8.1-64bit, exiftool-v12.11(standalone), sed-v4.0.7

StarGeek

Quote from: gonnhirrim on May 19, 2021, 09:34:50 AM
The issues with Powershell are not only linux-quoting, I had that figured out. But it needs special escaping, as is explained here: https://adamtheautomator.com/exiftool/#Issues_in_PowerShell.

Yeah, you can read my response to the many errors in that article here.  There were more, I believe, I just got tired of trying to correct them all.
* 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).