开发者

Code that finds missing files in a series

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开发者_StackOverflow中文版 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


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;
}


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";
    }
}


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜