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:

#!/usr/bin/perl
use File::Copy;

# parts from http://kangry.com/topics/viewcomment.php?index=289

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

@months =('01','02','03','04','05','06','07','08','09','10','11','12');
@days=('00','01','02','03','04','05','06','07','08','09','10',
'11','12','13','14','15','16','17','18','19','20',
'21','22','23','24','25','26','27','28','29','30',
'31','32','33','34','35','36','37','38','39','40',
'41','42','43','44','45','46','47','48','49','50',
'51','52','53','54','55','56','57','58','59','60');

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);
$year=$year+1900;
$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: $!";
}
}