Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi there.
I have just recently found myself in the realm of writing batch files, so bear with my in my ignorance.
I am currently attempting to write a batch file that will be placed within a specific directory, and then be executed, renaming all of the files within that directory.
I am using Photoshop Elements' batch processing to resize and rename all of my photos, and save them to the directory I specify. However, unlike Photoshop, Elements limits the user to only using two values when selecting a naming scheme. Thus, rather than run a second batch in Elements, I just want to set up a dos batch file that will add the third element to my filenames.
When Elements exports the new, resized files, it names them firstname_lastname#.jpg (i.e. John_Smith1.jpg). Now, for my website, I need both thumbnail versions and full size versions of the photos. Each photo is should be denoted by a "_lg" or "_sm" at the end of it, depending on it's size. Thus, the final filename should read John_Smith1_lg.jpg.
NOTE: I don't know if this matters or not, but some files I name with just firstname, so they would be John1_lg in the end.
Now, let me explain the process I am using to make this as efficient as possible, and perhaps we can add things to the batch file to speed things up even more.
1) I create a directory for someone's photos ( C:\John Smith ).
2) I place all of the original, unedited photos in this directory.
3) I run a batch file in Photoshop Elements that resizes, reduces resolution, and renames each file, and have it save it in a subdirectory named Edited. ( C:\John Smith\Edited )
4) Here is where I need the first dos batch file. It will rename the files to John_Smith1_lg.jpg.
5) If possible, I would like the batch file to also cut and paste the newly renamed files into the main John Smith directory.
6) Now I run the second batch in Elements, creating the thumbnail version of the photos, and again saving it into the "Edited" directory.
7) Here is where the second dos batch file is needed. It will rename the files to John_Smith1_sm.jpg. I would also like this second batch to cut and copy the files it has renamed and paste them into the main John Smith directory, and then delete the "Edited" directory completely, as it is no longer needed.One final note, the directory structure I am using for this project is not as simple as C:\foldername. It's actually L:\Folder1\Folder2\Folder3\Folder4\Folder5, where folders 3, 4 and 5 are different each time. Folder 5 is the directory I create to store the files, listed in Step 1 above. (I.e. John Smith).
Here is the part I wrote already:
===============
@echo off
RENAME *.jpg *_lg.jpg
===============But that didn't work, and doesnt do half the stuff I mentioned above... help this n00b out, please? :) I can provide you with any additional information you need, I just didn't know what was pertinant. Oh one thing I forgot, due to the LONG file structure I am using for this, I think my easiest course of action would be to simply copy and paste these bat files into each "Edited" directory, whenever I need it. That way I don't have to remember the entire directory address each time.

If you're interested in another option, I'd recommend using a Perl script instead of the batch file. With a simple Perl script you can not only handle the renaming of the files, but generate the thumbnails as well; bypassing the Photoshop Elements batch processing.
Here's an example that does one file; in your case we'd need to wrap this in a loop that will process all jpg's.
#!perl
use GD;
use Image::GD::Thumbnail;# Load your source image
open IN, 'E:/Images/test.jpg' or die "Could not open.";
my $srcImage = GD::Image->newFromJpeg(*IN);
close IN;# Create the thumbnail from it, where the biggest side is 50 px
my ($thumb,$x,$y) = Image::GD::Thumbnail::create($srcImage,50);
# Save your thumbnail
open OUT, ">E:/Images/thumb_test.jpg" or die "Could not save ";
binmode OUT;
print OUT $thumb->jpeg;
close OUT;

Hi Anthony,
The perl is probably better, but I can't help with that.
I'll need to recreate your "John Smith" structure and work on it, but for now...
The key syntax will involve the use of %%~NF, like this:
for %%F in (*.JPG) do echo %%~NF
which you'll see resolves to filename [only].
HTH
M2
Mechanix2@Golden-Triangle.com

Hi,
This will give you food for thought:
@echo off
md large
for /L %%N in (1 2 3) do type nul > %%N.jpg
for %%J in (*.JPG) do copy %%~NJ.jpg large\%%~NJ_LG.jpg
::--------------
M2Mechanix2@Golden-Triangle.com

All looking good so far, guys. I'm only able to do simple testing here at home under simulated circumastances, since this is for work, and I won't be there to test it for real until Monday.
Don't let that stop you from replying tho... :) Thanks for all you've done so far. I don't mind the Perl script, if thats what everyone thinks would be better.

Hi Anthony,
This is about the core of what you need. It will only work if run in the dir below the [customet name] dir.
You can probably figure out the delete/cleanup batch file. But be careful.
Here's your two 'renamers':
::-----------
:: large.bat
@echo offfor %%N in (1 2 3) do type nul > %%N.jpg
for %%J in (*.JPG) do copy %%~NJ.jpg ..\%%~NJ_LG.jpg
::-----
::small.bat
@echo offfor %%N in (1 2 3) do type nul > %%N.jpg
for %%J in (*.JPG) do copy %%~NJ.jpg ..\%%~NJ_SM.jpg
::------The second line in each is there for testing. {So there'll be a few jpgs to do something with.]
HTH
M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |