2010-07-18 62 views
0

我正在尋找一系列代碼來識別一系列文件中缺失的文件並將該列表導出到txt文件。例如:一個名爲1to100000的目錄包含名爲1,2 ... 99999,100000的pdf,但缺少系列中的一些。我希望腳本將這些丟失的文件報告給txt文件。理想情況下,這將是一個可執行的Perl腳本。 謝謝, 傑克查找系列文件中缺失文件的代碼

+0

請更清楚。當你說「一個名爲1to100000的目錄包含名爲1,2 ... 99999,100000的pdf」時,這些是PDF文件?什麼定義了這個系列?只有文件名或PDF文件需要解析才能定義系列?更好的問題產生更好的答案... – dawg 2010-07-18 05:30:40

+0

整數。該系列是一組pdf文件。目錄名稱並不真正相關。有一個文件夾包含文件x到x + 10000,這些約束很容易定義。如果我想知道從這個連續系列文件中缺少哪些文件,我該怎麼做? – Jake 2010-07-18 05:44:12

回答

3

只是從1到100000計數,並檢查,看看文件是否存在。

foreach my $num (1 .. 100000) { 
    my $fname = "1to100000/$num.pdf"; 
    print "missing $fname\n" unless -f $fname; 
} 
+0

我想你會發現100000個統計數據會比100000個readdirs慢很多。 – ysth 2010-07-18 15:56:55

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

這裏是(使用集:: 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; 
相關問題