Main Menu

String function

Started by Tarn, February 21, 2013, 05:43:40 PM

Previous topic - Next topic

Tarn

I'm having trouble understanding how to use "%f" to extract string values from a field, specifically filename. I can get it to work when I'm formatting a date. But to extract a portion of a text string to use in another field (tag) is still beyond me.

Examples of what I'm trying to do:
Take file name 121023-5432.jpg and put it into preservedfilename as 121023-5432.DNG
Take file name 121023-5432.jpg and put it into preservedfielname as PC105432.JPG
Take Dogshome-5432.jpg and rename it to Dogscamping-5432.jpg

I'm sure this will be simple once I figure it out; but right now it is whippin my butt!

Thanks in advance.

Phil Harvey

It's whipping your butt for good reason:  The %f (etc) formatting codes only apply when writing a file or directory name.  When writing other tags, there is no variable interpolation (otherwise, simple assignments could have surprising results).

But you are in luck.  ExifTool 9.15 introduced an advanced formatting feature.

See this thread for some reading on 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 ($).

Tarn

Okay, I read and played with it. Using
"-title<${filename;s/^.{11}(.*)\..*$/$1/}"
Seems to erase what is there. Changing the 11 {inside the braces} to 10 and continuing down to 8 do the same thing.  A 7 puts the last character, of 8, in. 6 puts the last two, 5 - 3, and so on until I leave the braces empty; that puts the entire filename, with extention in the preservedfilename field.

Using "%f", or "%%f" or any variations on it, I end up with "%%f.NEF", or "%-4f.NEF" in the preservedfilename field.

I understand that "%f" is for formatting what goes into a field; and not for what displays on my screen. I, also, understand that I cannot change case (from JPG to jpg, etc.) because Exiftool is not case sensitive. What I seem to be missing is how to trigger "%f" to work as a function and not as a string. I have tried everything that I can think of. Such as:
-preservedfilename"<$filename%-4f.NEF"
"-preservedfilename<$filename%-4f.NEF"
-preservedfilename"<$filename %-4f.NEF"
"-preservedfilename<$filename %-4f.NEF" to name a few.

What am I not understanding about the "%f" function. And yes, I've tried "%%f" as well.

Thank you for your patience.

Edit: Right! I just got it. I can only do this to the "filename" field, or tag... right?
Sorry, it took a while but it finally sunk in... right after I clicked "post".

Phil Harvey

Right.  %f only works where ExifTool is expecting a file name.

I'm still not really clear on what you want to do.  If you want to take the last 4 characters of the file name (as I deduce from your %-4f), then you could do this:  ${filename;$_=substr($_,-4)}

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

Tarn

I use preservedfilename to tell me where the image originated. In the case of a RAW file, the names will be identical; in the case of a JPG file -filename will be 110818-0448.jpg where -preservedfilename would be (should be) 110818-0448.NEF.

Primarily, what I want to do is to correct file names that are stored in "preservedfilename". An actual example is: image 110818-0448.jpg has "110818-0448.jpg" stored in "preservedfilename"; I want to correct that to "110818-0448.NEF". This will be helpful when I end up with an edited image named "Sunrise_in_Esparanza.jpg". In that particular case, I have a little over 80 files to choose from; and they are all quite similar. Being able to look into -preservedfilename and find out that it came from 110818-0448.NEF would be a wonderful things.

Eventually, I would like to be able to replace verbiage in a filename. An example would be to change Dogs-lake-1234.jpg to Dogs-camping-1234.jpg. For the times when I realize that I renamed 50 or so images incorrectly.

As for your example, I got it to work by removing the minus sign in front of the "4". That puts the filename, from the 4th character into the field. However, when I leave the minus sign in, the file gets renamed from 110818-0448.jpg to .jpg. That's right dot jpg, no filename, just the extension. ???

Here is what I typed in:
-filename"<${filename;$_=substr($_,-4)}" *.jpg
and
-filename"<${preservedfilename;$_=substr($_,-4)" *.jpg

Both result in the file ending up with the filename stripped out. I have even moved the quotes to enclose the entire statement; that does the same thing. I'm lost.


Phil Harvey

Quote from: Tarn on February 22, 2013, 01:12:35 PM
As for your example, I got it to work by removing the minus sign in front of the "4". That puts the filename, from the 4th character into the field. However, when I leave the minus sign in, the file gets renamed from 110818-0448.jpg to .jpg. That's right dot jpg, no filename, just the extension. ???

Ah right.  I forgot that FileName includes the extension.  So substr isn't all that applicable unless the extension is always 3 characters plus the ".", in which case you could use substr($_,-8,4) to take the last 4 characters of the file name without the extension.

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