Some format strings help

Started by lei, September 19, 2022, 09:37:51 AM

Previous topic - Next topic

lei

Dear Experts,

I'm learning ExifTool, and found there're several things I cannot find any help documents.

1. -d *FMT* and -c *FMT*. I think it should be the same format string, but I cannot find any detailed documents for that. On the ExifTool manual, it says *FMT* uses the same syntax as a "printf" format string. But what's printf? C's printf or something else.

2. The pattern matching format like this -if "$model=~/XXX XXX/". Also I cannot find any help document for this.

3. Regular Expression like this "-datetimeoriginal<${filename;s/WA.*//}".

Can anyone please refer me to the related help documents?

Thanks in advance!

Phil Harvey

#1
1.  -d uses strftime formatting.  Here is a man page for strftime.

-c uses printf formatting.  Here is a man page for printf.

But basically you use something like %.4f in the -c format string to represent a floating-point value with 4 digits after the decimal place.

2. The pattern matching is a Perl expression.  Here is a tutorial about this.

3. The regular expression is an advanced feature.  Here is some documention on this.  (StarGeek probably has better references for this.)

- 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: lei on September 19, 2022, 09:37:51 AM1. -d *FMT* and -c *FMT*. I think it should be the same format string, but I cannot find any detailed documents for that. On the ExifTool manual, it says *FMT* uses the same syntax as a "printf" format string. But what's printf? C's printf or something else.

They do not use the same format strings.  The -c (-coordFormat) option does use the printf format codes.  Since exiftool was written in Perl, you can look to the Perl's printf codes.  I thought printf codes were pretty standard across languages, but maybe I'm wrong.

The -d (-dateFormat) option uses the OS's strftime functions.  You can find the list under Common Date Format Codes.  These codes are system dependent as a few of them do not work on Windows.  Or at least some used to not work on Windows.  I don't know if Phil patched it or it was Win8 and earlier, but checking just now on Win10, all the codes seem to work.

Quote2. The pattern matching format like this -if "$model=~/XXX XXX/". Also I cannot find any help document for this.

3. Regular Expression like this "-datetimeoriginal<${filename;s/WA.*//}".

For the -if option, it is a"Perl-like logic expression".  Basically, you're using a bit of Perl code.  In this case it's the Perl binding operator, which is doing a RegEx match.

The second one is using a Perl RegEx substitution.

There's no additional documentation on these beyond what's in the main doc page.  These are more advanced operations and best learned through the many Perl and RegEx tutorials that can be found.  Trying to explain it all here would be just copy/pasting the entire PerlDoc website.

I learned most of my RegEx from Regular-Expressions.info and using sites like RegEx101 to test expressions.  Perl came in bits in pieces by searching for code in Perl to do what I wanted, mostly through StackOverflow.

Searching these forums also has plenty of examples.  I usually use Google with site:exiftool.org.
* 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).

lei

Hi Phil / StarGeek,

Thank you very much for your prompt reply. And also thank you very much for this wonderful tool. Just last year, I wrote some PowerShell scripts to help my wife's childcare centre organise children's photos and videos. If I had known ExifTool, I could have saved lots of time :'( .

I'll follow your instructions and read the documents. If I have any more question, I'll come back and ask.

BTW, I know regex quite well. I just want to know what dialect ExifTool uses. For example, when I try to use ^ and $, it doesn't accept. That's why I'd like to see its guide.

Thanks once again!

Phil Harvey

#4
Quote from: lei on September 19, 2022, 11:08:00 AMBTW, I know regex quite well. I just want to know what dialect ExifTool uses. For example, when I try to use ^ and $, it doesn't accept. That's why I'd like to see its guide.

Good point.  If you are talking about the -if option syntax, they actually do work but since ExifTool pre-processes the string to parse the tag-name variables, the "$" is special.  This is explained in the -if option documentation:

            3) Tags in the string are interpolated in a similar way to -p
            before the expression is evaluated.  In this interpolation, $/ is
            converted to a newline and $$ represents a single "$" symbol.  So
            Perl variables, if used, require a double "$", and regular
            expressions ending in $/ must use $$/ instead.


- 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

Also, you need to make sure you're using the correct quotes.  The caret ^ is an escape symbol for Windows and the dollar sign $ is treated as a variable if the quoting is not correct.  With Powershell, I believe you need to use single quotes.
* 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).

Phil Harvey

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

lei

Hi Phil / StarGeek,

Thank you both once again. You guys are really great!!

lei

Hi Phil / StarGeek or Other Experts,

I decided to ask another similar question. This time is the -p string format.

Initially, I thought it can only take ordinary text and tag names (with $). But later, I found it can have $$ for $, and $/ for newline. But what is for a tab, etc? Is there any other things we can use within the format string? Any document you can refer me?

Thanks in advance!

StarGeek

The -p (-printFormat) option docs pretty much covers it all.

If you need to use a tab on Windows, you probably have to resort to creating a FMT file since Windows doesn't seem to deal well with a tab. I tried copy/pasting a tab from NotepadL++. CMD didn't register it and while PS showed it, the output was changed to spaces rather than tabs.
* 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).

lei

Hi StarGeek,

Thank you again for your prompt reply! Really appreciate it.

I actually don't use -p quite often as I usualy don't need to display lots of info. I was just curious to see how many other things I can use the -p option to do when I saw $$ and $/.

Thanks!