Batch file to write from txt file

January 23, 2011 at 02:48:02
Specs: Windows Vista
Dear Friends,

I have the following batch file, which successfully writes from the file names in the directory to the caption and comment in jpeg files.

@echo off
echo Running MakeCaption
echo.
SETLOCAL ENABLEDELAYEDEXPANSION

REM these four lines empty the variables, except the last which is set to zero
SET FILE=
SET CAP=
SET COM=
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 from the 2nd character upto 4 before the end. Example: 001 Reclining Buddha, Chaukhtatgyi, Yangon.jpg becomes: 01 Reclining Buddha, Chaukhtatgyi, Yangon - if you want the whole filename (excluding the extension, change the following line to: SET CAP=!FILE:~1,-4!
SET CAP=!FILE:~2,-4!
echo. !CAP!
REM this sets the comment. Here I am taking the name from the 5th character upto 4 before the end. Example: 001 Reclining Buddha, Chaukhtatgyi, Yangon.jpg becomes: Reclining Buddha, Chaukhtatgyi, Yangon
SET COM=!FILE:~5,-4!

REM this is writing the caption if you have exiv2.exe 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 it could be rewritten: "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!"
SET FILE=
SET CAP=
SET COM=
)

I have another scenario where I need to write the captions and comments from a pre-prepared txt file which is in the same directory. There is one line of text to each of the files in the folders.

Can someone show me how to change the script to accomplish this?


See More: Batch file to write from txt file

Report •

#1
January 23, 2011 at 11:24:06
i m not too sure if i understand what you are asking correctly.
However you can try this workaround, it would be a lenthy one but should work.

from the pre-prepared text file extract each lines and store in a variable or a text file with numbering in temp directory,Text file sounds more reliable.
For example, First append a line number before each line in the text file using findstr /v /n "anyoddtihng".
then parase this new text file using for loop and put the output to a new text files in temp directory with a number prefix same as the line number.
now using final loop where you are putting the caption get the value from text files from temp.


May be someone can come up with a better idea

Subhash Chandra.
www.s-chandra.co.cc


Report •

#2
January 23, 2011 at 16:22:11
Dear Subash, thanks for the reply, and I'm sorry if it wasn't clear. What I am trying to do is find a way to replace this line:

FOR %%f IN (*.jpg) do (

with a line that reads the txt file and extracts the info from there line by line until the end of the file (which has the same number of lines as there are jpegs).


Report •

#3
January 23, 2011 at 17:46:12
FOR /f "tokens=*" %%a IN (myfile) DO (
what do do with the line...
)

Can I put a question here?


Report •

Related Solutions

#4
January 23, 2011 at 18:33:53
Thanks Matt, that reads the txt file correctly - the only problem is the code doesn't know what file to write to now.

Sorry I didn't think of that, it is coming at the end of the following line:

"c:\program files\exiv2\exiv2" -k -M "set Iptc.Application2.Caption !CAP!" "%%f"

So I need to do something more, not only read the txt file to set up the caption, but retrieve the filename I am writing to, or at least get the files in the directory one by one. Any ideas?


Report •

Ask Question