So, I have a big directory full of images. The naming pattern is <Number>.<Counter>.jpg (where counter is 000, 001, 002...). I have a list of numbers which match up with Numbers that I want to keep (so for a given Number in the list, keep Number.000.jpg, Number.001.jpg, etc).
As far as I can tell, the following code should work, but the numbers it reports don't match up. Any idea what's going on?
The current output is:
33600 images in photos (8321 unique). 6571 numbers in numbers_only.txt. 5999 numbers slated for deletion.
#!/usr/bin/perl -w
use strict;
my $num_file = "numbers_only.txt";
my %currentlist;
my $i = 0;
open(CURRFILE, "<$num_file") or die "couldn't open the num file: $!\n"
+;
while (<CURRFILE>) {
chomp;
$currentlist{$_}++;
$i++;
}
close CURRFILE;
my $dir = "images/photos";
opendir FILEDIR, $dir or die "Couldn't open $dir: $!\n";
my @filelist = grep !/^\.\.?$/, readdir FILEDIR;
closedir FILEDIR;
my %uniquelist;
my %deletelist;
my $h = 0;
foreach my $pic (@filelist) {
next unless ($pic =~ /jpg$/);
$pic =~ s/^(\d+).*/$1/;
$h++;
$uniquelist{$pic}++;
next if ($currentlist{$pic});
$deletelist{$pic}++;
}
my $output = "delete_list.txt";
open OUT, ">$output" or die "Couldn't open output: $!\n";
my $j = 0;
foreach my $del (sort keys %deletelist) {
print OUT "$del\n";
$j++;
}
close OUT;
my $k = 0 + keys %uniquelist;
print "$h images in photos ($k unique). $i numbers in numbers_only.txt
+. $j numbers slated for deletion.\n";
exit;