Hello, I'm looking for a line of code that identifies missing files in a series of files and exports that list to a txt file. For example: a directory called 1to100000 contains pdfs named 1,2...99999,100000 but is missing some from the series. I would like the script to report those missing files to a txt file. Ideally this would be an executable perl script. Thanks, Jake
+4
A:
Just count from 1 to 100000 and check to see if the file exists.
foreach my $num ( 1 .. 100000 ) {
my $fname = "1to100000/$num.pdf";
print "missing $fname\n" unless -f $fname;
}
friedo
2010-07-18 05:44:50
I think you'll find 100000 stats will be substantially slower than 100000 readdirs.
ysth
2010-07-18 15:56:55
+3
A:
Using readdir:
my @expect = map "$_.pdf", 1..100000;
my %notfound;
@notfound{@expect} = ();
opendir my $dirh, "1to100000" or die "Couldn't open directory: $!";
while ( my $fname = readdir($dirh) ) {
delete $notfound{$fname};
}
for my $fname (@expect) {
if ( exists $notfound{$fname} ) {
print "missing $fname\n";
}
}
ysth
2010-07-18 16:20:49
A:
Here is an example of finding missing numbers in a range (Using Set::IntSpan).
#!/usr/bin/perl
use strict;
use warnings;
use Set::IntSpan;
# the last sector on disk
my $end_sect = 71127179;
# The complete range of sectors on the disk
my $range = Set::IntSpan->new( "0-$end_sect" );
# The ranges of used sectors
my $used = Set::IntSpan->new(
'0-1048706,1048707-2097414,69078879-71127179' );
# Calculates the remaining unused sectors
my $unused = $range->diff( $used );
print $unused->run_list;
Chris Charley
2010-07-19 15:52:50