I ran your script in a directory where there were multiple image file types, .bmp, .jpeg., etc. After this, I had exactly one remaining file for each image file type...the rest were quashed. Perhaps try adding an element of uniqueness when renaming your files, such as by adding a timestamp. I'm not sure of your exact circumstances, but this issue may only pop up when dealing with the large number of files you are working with. Try the following:
foreach my $file (@files)
{
my $ext;
$file =~ m/(.[^.]+)$/;
$ext = $1;
my $orig = $file;
$orig =~ s/\..*$//;
rename $file, time() . "($orig)" . $ext or die $!;
}
hope this helps. cheers.