2013-08-01 24 views
0

使文件之間的差異文件大小的基礎上,我寫一個腳本,我有3個一級目錄如下圖所示:如何在Perl

LOG 
├── a.txt 
├── b.txt 
└── sdlog 
    ├── 1log 
    │ ├── a.txt 
    │ └── b.txt 
    └── 2log 
     ├── a.txt 
     └── b.txt 

的文件的名稱是相同的,但規模將始終差異。我必須在LOG dir和1log dir的大小的基礎上比較這些文件。那些在2log中的文件我們不會做任何事情。

我已經寫了這些打印的文件名稱,但不能以上任務的腳本:

#!/usr/bin/perl 
use strict; 
use warnings; 
use File::Find; 
use File::Basename; 
my $new_file_name; 
my $start_directory = "C:\\logs"; 


find({ wanted => \&renamefile }, $start_directory); 
sub renamefile 
{ 
    if (-f and /\.txt$/) 
    { 
    my $file = $_; 
    open (my $rd_fh, "<", $file); 
    LINE: while (<$rd_fh>) 
    { 
     if (/<(\d)>/i) 
     { 
     close $rd_fh; 
     print"$file\n"; 
     #print" Kernal-> $file\n"; 
     last LINE; 
     } 
    if (/I\/am_create_activity/i) 
    { 
    close $rd_fh; 
    print"$file\n"; 
     #print" EVENT-> $file\n"; 
    last LINE; 
    } 
    } 
} 
}  
+0

使用'-s'來獲取文件的大小。請說明你想達到的目標。 – choroba

+0

我的目的是將Dir-> LOG-> a.txt,b.txt與subdir-> 1log-> a.txt,b.txt進行比較。文件大小我想要那個文件。 – Maverick

回答

3

使用-s來獲取文件的大小。由於您不是遞歸搜索子目錄,因此您不需要File::Find

#!/usr/bin/perl 
use warnings; 
use strict; 

use File::Basename; 

my $path1 = 'LOG'; 
my $path2 = 'LOG/sdlog/1log'; 

for my $file (glob "$path1/*.txt") { 
    my $name = basename($file); 

    if (-f "$path2/$name") { 

     if (-s $file > -s "$path2/$name") { 
      print $file, "\n"; 

     } else { 
      print "$path2/$name\n"; 
     } 

    } else { 
     warn "File $path2/$name not found.\n"; 
    } 
}