Organizing files by date with perl

Organize over 4200 files in 2.5 seconds!
I have taken a ton of photos since I got my new camera last December (4265 files, to be exact) and decided that I need to sort through and organize. I have them stored on my server at home and accessible from my iBook (which has Adobe CS2 on it.) When viewing that directory in Adobe Bridge, the laptop chugs and chugs and never seems to be able to catch up with generating all 4265 previews/thumbnails. So I decided to reorganize the photos into smaller, more manageable directories.

I hacked together a simple perl script to sort them all out by the file’s timestamp. I hope this will make things more manageable while I sort through all of the images.
What it does is it finds each file’s timestamp and creates a directory for that date (i.e. 20070605) and then moves the file into that directory. Once again, I’m just glad that I don’t have to manually sort through and organize all of these files by date. In 2.5 seconds, I had my files split up into 92 manageable directories.
Here’s the code:

use File::Copy;

# parts from

opendir(DIR, ".");
@files = readdir(DIR);

@months =('01','02','03','04','05','06','07','08','09','10','11','12');

foreach $file (@files) {
if ( -f $file ) {
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)= stat($file);
($sec, $min, $hour,$mday, $mon, $year, $wday, $yday, $isdst) =localtime($mtime);
$formatted_date =$year.$months[$mon].$days[$mday];
if (!( -d $formatted_date )) {
mkdir($formatted_date, 0755) || die "Cannot mkdir newdir: $!";
print "moving $file to $formatted_date\n";
move("$file", "$formatted_date/$file") or die "move failed: $!";