Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Renaming files using a batch

Original Message
Name: averacruz
Date: December 10, 2004 at 12:31:19 Pacific
Subject: Renaming files using a batch
OS: Windows 2000
CPU/Ram: Intel P3, 256MB
Comment:
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.


Report Offensive Message For Removal


Response Number 1
Name: FishMonger
Date: December 10, 2004 at 17:30:44 Pacific
Subject: Renaming files using a batch
Reply: (edit)
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;


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: December 10, 2004 at 19:03:23 Pacific
Subject: Renaming files using a batch
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: December 10, 2004 at 19:52:45 Pacific
Subject: Renaming files using a batch
Reply: (edit)
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

::--------------


M2

Mechanix2@Golden-Triangle.com


Report Offensive Follow Up For Removal

Response Number 4
Name: averacruz
Date: December 10, 2004 at 22:59:02 Pacific
Subject: Renaming files using a batch
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: December 11, 2004 at 04:22:07 Pacific
Subject: Renaming files using a batch
Reply: (edit)
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 off

for %%N in (1 2 3) do type nul > %%N.jpg

for %%J in (*.JPG) do copy %%~NJ.jpg ..\%%~NJ_LG.jpg
::-----
::small.bat
@echo off

for %%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


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Renaming files using a batch

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC