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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|