2014-01-07 27 views
2

我經歷了一堆問題以找到從目錄中獲取文件名稱的最佳方式。但是,我有一個奇特的場景,需要一些幫助。是從perl中的目錄中拾取文件

我direcotry的文件如下

-rw-rw-r-- 1 root 55000 53916 Apr 12 2013 Update_2013-04-12_02-17-55.txt 
-rw-rw-r-- 1 root 55000 53916 Apr 12 2013 UpdateCIMS_2013-04-12_03-20-30.txt 
-rw-rw-r-- 1 root 55000 53763 Apr 15 2013 UpdateCIMSFlag_2013-04-15_05-47-41.txt 
-rw-rw-r-- 1 root 55000 91981 Apr 23 2013 UserManagementService_2013-04-23_03-55-52.txt 
-rw-rw-r-- 1 root 55000 92076 Apr 23 2013 UserManagementService_2013-04-23_04-34-42.txt 
-rw-rw-r-- 1 root 55000 92086 Apr 23 2013 UserManagementService_2013-04-23_23-55-10.txt 
-rw-rw-r-- 1 root 55000 91971 Apr 24 2013 UserManagementService_2013-04-24_02-23-20.txt 
-rw-rw-r-- 1 root 55000 59441 Apr 24 2013 SecuredService_2013-04-24_02-29-08.txt 
-rw-rw-r-- 1 root 55000 42240 May 20 2013 UpdateCIMSFlag_2013-05-20_04-24-19.txt 
-rw-rw-r-- 1 root 55000 40547 May 20 2013 UpdateCIMSFlag_2013-05-20_05-31-29.txt 
-rw-rw-r-- 1 root 55000 42238 May 20 2013 UpdateCIMSFlag_2013-05-20_05-43-54.txt 
-rw-rw-r-- 1 root 55000 59493 May 21 2013 SecuredService_2013-05-21_04-25-32.txt 
-rw-rw-r-- 1 root 55000 88374 May 21 2013 RegistrationService_2013-05-21_23-55-33.txt 
-rw-rw-r-- 1 root 55000 88426 May 22 2013 RegistrationService_2013-05-22_00-20-04.txt 
-rw-rw-r-- 1 root 55000 60014 Jul 31 04:16 SecuredService_2013-07-31_04-16-56.txt 
-rw-rw-r-- 1 root 55000 91636 Sep 2 06:11 AdminServices_2013-09-02_06-11-17.txt 
-rw-rw-r-- 1 root 55000 91649 Sep 3 05:37 AdminServices_2013-09-03_05-37-54.txt 
-rw-rw-r-- 1 root 55000 133629 Sep 3 05:43 UserManagementService2_2013-09-03_05-43-56.txt 
-rw-rw-r-- 1 root 55000 556 Sep 9 08:26 Test_2013-09-09_08-26-23.txt 
-rw-rw-r-- 1 root 55000 556 Sep 9 08:37 Test_2013-09-09_08-37-20.txt 
-rw-rw-r-- 1 root 55000 133708 Sep 13 02:28 UserManagementService2_2013-09-13_02-28-49.txt 
-rw-rw-r-- 1 root 55000 60107 Sep 13 02:30 SecuredService_2013-09-13_02-30-43.txt 
-rw-rw-r-- 1 root 55000 133743 Sep 13 04:44 UserManagementService2_2013-09-13_04-44-29.txt 
-rw-rw-r-- 1 root 55000 100886 Sep 16 04:27 AdminServices_2013-09-16_04-27-33.txt 
-rw-rw-r-- 1 root 55000 556 Sep 20 06:40 Test_2013-09-20_06-40-16.txt 
-rw-rw-r-- 1 root 55000 110236 Nov 25 02:35 AdminServices_2013-11-25_02-35-37.txt 
-rw-rw-r-- 1 root 55000 142357 Dec 18 03:13 UserManagementService2_2013-12-18_03-13-20.txt 

正如你所看到的,我有不同的時間戳和不同的文件類似的文件。所以我需要類似的文件名,不包括時間戳和最新的文件。我希望我的最終結果顯示帶有時間戳的最新唯一文件名。

我正在嘗試opendir,但沒有看到任何結果。

#!/usr/bin/perl 
use File::stat; 
my $DIR = "/home/DIR"; 
opendir(my $DH, $DIR) or die "Error opening the dir"; 
my %files = map { $_ => (stat("$DIR/$_"))[9] } grep(! /^\.\.?$/, readdir($DH)); 
closedir($DH); 
my @sorted_files = sort { $files{$b} <=> $files{$a} } (keys %files); 
print $_; 

請幫忙。 我期待的輸出是

AdminServices_2013-11-25_02-35-37.txt 
UserManagementService2_2013-12-18_03-13-20.txt 
SecuredService_2013-09-13_02-30-43.txt 
RegistrationService_2013-05-22_00-20-04.txt 
etc... 
+0

哪個時間戳的優先級,從一個'stat'或文件名? –

+0

他們都是一樣的。但是需要文件名。 – user3164754

回答

0
#!/usr/bin/perl 
my $DIR = "/home/DIR"; 
opendir(my $DH, $DIR) or die $!; 

my %files; 
while (my $f = readdir($DH)) { 
    next if $f =~ /^\.\.?$/; 

    my ($key, $t) = split /_/, $f, 2; 
    # @{ $files{$key} }{ "t","f" } = ($t, $f) 
    # if !$files{$key} or $files{$key}{t} lt $t; 
    my $h = $files{$key} ||= {}; 
    if (! %$h or $h->{t} lt $t) { 
    $h->{t} = $t; 
    $h->{f} = $f; 
    } 
} 

print "$files{$_}{f}\n" for sort keys %files; 
+0

這工作。我添加了下面的代碼。 foreach my $ key(sort {$ files {$ b} <=> $ files {$ a}}(keys%files)){print「$ files {$ key} {f} \ n」} 我們可以得到輸出文件名的順序而不是時間戳? – user3164754

+0

@ user3164754輸出已按文件名升序排列。 'for(sort keys%files){print「$ files {$ _} {f} \ n」}' –

+0

感謝您的幫助。 – user3164754

1

一開始,opendir是沒問題的。您沒有正確使用打印語句。

foreach(@sorted_files) 
{ print $_ . "\n"; } 

輸出文件名。這只是讓你獲得一些輸出的開始。我沒有完成這個問題。

0

你可以使用水珠:

my $filespec = "/tmp/test*"; 
my %files; 
while (my $file = glob("$filespec")){ 
    next if $file eq '.' or $file eq '..'; 
    next if ! -f $file; 
    my $datestamp = (stat($file))[9]; 
    #remove timestamp 
    my $filename = $file;  
    $filename =~ s!_\d{4}-\d{2}-\d{2}_\d{4}-\d{4}-\d{4}\.\W{3}\z!!is; 
if (! exists $files{$filename} || $files{$filename}<$datestamp){ 
    $files{$filename} = $datestamp; 
    $files{$filename} = $datestamp; 
}     
} 
foreach my $key (sort { $files{$b} <=> $files{$a} } (keys %files)){ 
    print "$key\t$files{$key}\n"; 
}