Copy directory's name, not its path, to IPTC:Caption-Abstract of its contents

Started by millerstevew, August 10, 2013, 08:49:49 PM

Previous topic - Next topic

millerstevew

I would like to copy the directory name, not path, to the IPTC:Caption-Abstract field of its contents.

When I run the following command:

exiftool '-iptc:caption-abstract<directory' /Users/steve/Desktop/School\ Photos/School\ Photos\ \(1\).jpg

Exiftool copies the directory path to the IPTC:Caption-Abstract field of its contents, in this case "/Users/steve/Desktop/School Photos". How can I copy just the directory's name instead?

I've been reading the exiftool Application Documentation, and I found this information. Unfortunately, I'm having trouble deciphering it.

QuoteA substring of the original file name, directory or extension may be taken by specifying a field width immediately following the '%' character. If the width is negative, the substring is taken from the end. The substring position (characters to ignore at the start or end of the string) may be given by a second optional value after a decimal point. For example:

    Input File Name     Format Specifier    Output File Name
    ----------------    ----------------    ----------------
    Picture-123.jpg     %7f.txt             Picture.txt
    Picture-123.jpg     %-.4f.out           Picture.out
    Picture-123.jpg     %7f.%-3f            Picture.123
    Picture-123a.jpg    Meta%-3.1f.txt      Meta123.txt
For %d, the field width/position specifiers may be applied to the directory levels instead of substring position by using a colon instead of a decimal point in the format specifier. For example:

    Source Dir     Format   Result       Notes
    ------------   ------   ----------   ------------------
    pics/2012/02   %2:d     pics/2012/   take top 2 levels
    pics/2012/02   %-:1d    pics/2012/   up one directory level
    pics/2012/02   %:1d     2012/02/     ignore top level
    pics/2012/02   %1:1d    2012/        take 1 level after top
    /Users/phil    %:2d     phil/        ignore top 2 levels
(Note that the root directory counts as one level when an absolute path is used as in the last example above.)

---

Also, all of these folders are prefixed with a date. Is it possible to copy everything after the 17th character of a directory's name to the IPTC:Caption-Abstract field of its contents.

An example folder name is: "2004.06.09 - 1 - School Photos". I would like to move "School Photos" to the IPTC-caption-abstract field.

Thanks again Phil. I love your tool.

- Steve

Phil Harvey

Hi Steve,

The %d, %f and %e apply only where a file name is expected, so they don't help here.

Instead, the alternative is to use the advanced formatting feature, which can be a bit tricky.

Here is a command that should take everything after the date in the directory:

exiftool '-caption-abstract<${directory;s/.*\d{4}-\d{2}-\d{2} \d+ //}' DIR

Notes:

1) the above quoting is for Mac/Linux.  Use double quotes instead in Windows.

2) if the subsitution pattern doesn't match, then the full directory name will be written.

To learn more about substitution expression, Google "Perl regular expressions".

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

millerstevew

Phil,

Thanks for the help. I Googled "Perl regular expression substitutions" and found a wealth of information.

Until an hour ago, I knew absolutely nothing about Perl. Now, I know some--albeit very little--about Perl regular expressions and substitutions. What fun! Thanks for giving me the challenge.

Since I use periods to separate my date values, not hyphens, I had to change the code you gave me slightly. Here's what I came up with:

exiftool '-iptc:caption-abstract<${directory;s/.*\d{4}.\d{2}.\d{2} \- \d* *\-* *//}' '-comment<${directory;s/.*\d{4}.\d{2}.\d{2} \- \d* *\-* *//}' -overwrite_original FILE

It works like a charm.

- Steve

StarGeek

Quote from: millerstevew on August 11, 2013, 03:46:05 PM

Since I use periods to separate my date values, not hyphens, I had to change the code you gave me slightly. Here's what I came up with:

The dot is a wildcard in regular expressions and matches any single character.  In the places you want it to exactly match a dot, you'll want to put a backslash before it (\.).   For example, use ${directory;s/.*\d{4}\.\d{2}\.\d{2} \- \d* *\-* *//}
* 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).