Seek batch file to "save as" many files from one per txt db

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JohnDOS
Posts: 2
Joined: 07 Apr 2013 21:36

Seek batch file to "save as" many files from one per txt db

#1 Post by JohnDOS » 07 Apr 2013 21:58

Hello,
I've never written a batch file but I think my task is very straightforward and should be easy to code for batch.

I have a text file called names.txt with one name per line.
I have a .jpg file named template.jpg
These two files are in the project folder.

I want to have a batch file that I can put into that project folder and that when activated from there, will open the text file, then take the first line of the text file as a variable, then do a "save as" operation on the template.jpg file whereby the template.jpg file is saved as a new .jpg file named exactly as the first line of text from the text file.

Then to continue this function for as many names there are in the txt file, then stop.

So, on completion, the folder will contain the batch file, the text file, the original template.jpg file and one additional jpg file for each line of text in the text file.

Example:

Folder contains three files:

1. BatchSaveAS.bat batch file that I'm trying to create
2. Names.txt
3. Template.jpg

Names.txt contains following text:

John Doe
Mary F. Price
Karl Jones Jr.

Then, after running the batch file, the project folder would contain the following files:

BatchSaveAs.bat
Names.txt
Template.jpg
John Doe.jpg
Mary F. Price.jpg
Karl Jones Jr..jpg

It is important to note that the names contain spaces and periods.

Any help appreciated!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Seek batch file to "save as" many files from one per txt

#2 Post by abc0502 » 07 Apr 2013 22:38

Try This (test it first)

Code: Select all

@Echo OFF

SET "NamesFile=Names.txt"
SET "ImageName=Template.jpg"

For /F "delims=" %%A In ('Type "%NamesFile%"') Do (
   COPY "%~dp0%ImageName%" "%%A.jpg" >NUL
   )
pause

JohnDOS
Posts: 2
Joined: 07 Apr 2013 21:36

Re: Seek batch file to "save as" many files from one per txt

#3 Post by JohnDOS » 07 Apr 2013 22:43

To abc0502
I tested it and it worked perfectly on my test input file of two names.
Thank you!
John

Ref:
@Echo OFF

SET "NamesFile=Names.txt"
SET "ImageName=Template.jpg"

For /F "delims=" %%A In ('Type "%NamesFile%"') Do (
COPY "%~dp0%ImageName%" "%%A.jpg" >NUL
)
pause

Post Reply