我正在寻找一行代码来识别一系列文件中丢失的文件并将该列表导出到 txt 文件。例如:名为 1to100000 的目录包含名为 1,2...99999,100000 的 pdf,但该系列中缺少一些。我希望脚本将那些丢失的文件报告到 txt 文件中。理想情况下,这将是一个可执行的 perl 脚本。谢谢,杰克
问问题
177 次
3 回答
3
只需从 1 数到 100000 并检查文件是否存在。
foreach my $num ( 1 .. 100000 ) {
my $fname = "1to100000/$num.pdf";
print "missing $fname\n" unless -f $fname;
}
于 2010-07-18T05:44:50.207 回答
3
使用 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";
}
}
于 2010-07-18T16:20:49.750 回答
0
下面是一个在范围内查找缺失数字的示例(使用 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;
于 2010-07-19T15:52:50.883 回答