Writing Filenames into JPEG Comments and Captions with a Batch File

For a long time I have had a problem: I use the free Picasa software and the accompanying Picasa web albums for publishing some of my photographs.

The problem is this: you have to write the caption for the photograph into the Picasa software by hand – well, my friends, when you have a lot of photographs that takes a long time, so knowing there must be one šŸ™‚ I looked for a solution.

Now my files are already well named, with a 3-digit number followed by a descriptive name. In Explorer (actually FreeCommander) they look like this:

File Names in Explorer

What I wanted was to write the name from the file to the caption.

After sufficient searching I found someone who had fixed the same problem with a batchfile, which can be found at this address. It relies on downloading a C++ library and command line utility, Exiv2.

You will need to download that utility in a zipfile and put it somewhere on your hard-disk. For me the logical place was in my progam files: c:\program files\exiv2\

A problem with the script was it didn’t quite suit what I needed and so I hacked it a little to get it to fit, and remove the first character but leave the rest. After running it the Picasa software files updated, so they looked like this (the numbers are there to keep them in good order):

Caption Names in Picasa

It then occurred to me that I could/should also be able to write direct to the JPEG comment also, so I contacted the maker of Exiv2 and after some short correspondence I came up with a batch file, that writes both the caption and the comment in one run.

Comment Name in FastStone Image Viewer


Here is the heavily annotated script (REM = remark or note, it is not passed to the interpreter):

[batch]

@echo off
echo Running MakeCaption
echo.
SETLOCAL ENABLEDELAYEDEXPANSION

REM these lines empty the variables
SET FILE=
SET CAP=
SET COM=

REM setting the counter to zero
SET COUNT=0

REM the loop starts here
FOR %%f IN (*.jpg) do (
SET FILE= !FILE!%%f

REM this sets the caption. Here I am taking the name
REM from the 2nd character upto 4 before the end.
REM Example: "001 Reclining Buddha, Chaukhtatgyi, Yangon.jpg"
REM becomes: "01 Reclining Buddha, Chaukhtatgyi, Yangon"
REM if you want the whole filename (excluding the extension)
REM change the following line to: SET CAP=!FILE:~1,-4!
SET CAP=!FILE:~2,-4!

REM this sets the comment. Here I am taking the name
REM from the 5th character upto 4 before the end.
REM Example: "001 Reclining Buddha, Chaukhtatgyi, Yangon.jpg"
REM becomes: "Reclining Buddha, Chaukhtatgyi, Yangon"
SET COM=!FILE:~5,-4!

REM this is writing the caption if you have exiv2.exe
REM somewhere else you will need to change the address
"c:\program files\exiv2\exiv2" -k -M ^
"set Iptc.Application2.Caption !CAP!" "%%f"

REM this is writing the comment if you wanted to include a name
REM it could be rewritten:
REM "c:\program files\exiv2\exiv2" -c "!COM! by NAME" "%%f"
"c:\program files\exiv2\exiv2" -c "!COM!" "%%f"

REM increase the number variable and reset the others
SET /a COUNT=!COUNT!+1
echo Caption #!COUNT! = "!CAP!"

REM emptying the variables for the next round
SET FILE=
SET CAP=
SET COM=
)

[/batch]

For it to work you have to copy the lines above into a text file, and save it as Caption.bat (or some such name, but with the .bat extension) and move the batchfile to the directory you want to change, and double-click it.

It will only change metadata in the jpgs, not the photograph, but note it will change all the jpgs in the directory.

I hope this might help someone having the same problem – tomorrow I will show how to use much the same technique to write tags into mp3 files.

 




Possibly Related Posts:


2 comments to Writing Filenames into JPEG Comments and Captions with a Batch File

  • Alexander

    Hi! Very useful info. Based on your script I wrote a version for Total Commander.
    Just add a button with param %L in it to run the script.


    @echo off
    echo Running AddJpegComment
    echo.
    SETLOCAL ENABLEDELAYEDEXPANSION

    rem chcp 1251

    SET tempFilePath=%1
    Set count=0

    for /f "tokens=*" %%f in (%tempFilePath%) do (
    SET fPath=%%f
    for %%N in ("!fPath!") do (
    SET fName=%%~nxN
    Set fExt=%%~xN
    )
    for %%i in (".jpg", ".jpeg", ".JPG", "JPEG") do (
    if %%i == "!fExt!" call :addComment "!fName!" "!fPath!"
    )
    Set /a count=!count!+1
    )

    echo Files processed: !count!
    pause

    :addComment
    echo %1
    "d:\Praeeo\Install\exiv2-0.23-win\exiv2" -c %1 %2
    exit /b
    )

  • Anandajoti

    Thank you Alexander!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>