Re: Read Text File to Rename Images - Please help! [A bit OT]
Re: Read Text File to Rename Images - Please help! [A bit OT]
- Subject: Re: Read Text File to Rename Images - Please help! [A bit OT]
- From: Paul McCann <email@hidden>
- Date: Thu, 06 Sep 2001 11:56:47 +0930
Ray asked...
>
I have just learned tonight I will have to rename *all* of the original web
>
files on our server, from our current part number (SKU) to another number
>
(EBU #) for our new e-commerce system. Of this, there are close to 3,269
>
give or take a few jpgs. Some will have to be re-shot.
Sounds like one of those horrible moments when your whole body experiences
that "sinking feeling". I'm going to risk the language police coming down on
me like a ton of bricks (yep, I know this is an applescript forum), but what
the heck: Applescript is great for some jobs, but I think this sort of thing
is best done by another tool. If you have Macperl installed --or either (i)
you are not averse to spending five minutes installing it, or (ii) you are
sufficiently desperate!!-- I think the following script will do pretty much
exactly what you've specified. Usual caveats about backup copies of
everything should be applied in the usual manner! (Please see Ray's original
message in #1035 for an exact description of what he was seeking.)
Obviously the same thing could be done in applescript, but I bet it's
truckloads quicker in macperl. I've written the thing with a view to
understandability instead of conciseness/cuteness in the hope of providing
an example of how perl scripts needn't be unreadable. With any luck some
people might find it useful in other situations. It's a bit crude in that it
makes two independent mappings; one of the old name to the new names of the
files, and another that's used in the duplication of the files. Some memory
could definitely be saved there, but it's not going to be a big issue for
circa 3000 files.
Obviously you'll need to change the hard-coded locations of the
"name_maps.txt" file and the "images" folder. If you have any problems with
it (or can't get it to run properly, or don't see what's being done) feel
free to contact me off-list.
Regards,
Paul
--------------------------------------------------------------------------
#!perl -w
use strict;
use File::Copy; # this brings in the "copy" routine used below
my $flatfile='Macintosh HD:Desktop Folder:name_maps.txt';
my $image_dir='Macintosh HD:Desktop Folder:images:';
my (%mapped_to,%copy_to); # hashes to hold the name maps
open IN,$flatfile or die "where is my input?: $!";
while (<IN>){
chomp; #eat the new line on the end of the input
my ($before,$after)=split /,\s*/,$_; #split on the comma+space
$copy_to{"$before.jpg"}="p${after}s.jpg"; #the name of the copy
$mapped_to{"$before.jpg"}="p${after}a.jpg"; #the renamed file
}
chdir($image_dir) or die "could not change directory:$!";
opendir DIR, $image_dir or die "where are my images?: $!";
foreach my $image_file (readdir DIR){
# only rename files with a mapping defined in the flat file
next unless $mapped_to{$image_file};
copy("$image_file","$copy_to{$image_file}")
or warn "Could not copy the file $image_file: $!";
rename("$image_file","$mapped_to{$image_file}")
or warn "Could not rename the file $image_file: $!";
}