opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
next if ($file !~ /\.txt$/i);
my $mtime = (stat($file))[9];
print $mtime;
print "\n";
}
基本上我想記下目錄中所有txt文件的時間戳。如果有一個子目錄我想在該子目錄中包含文件。如何遍歷目錄中的所有文件;如果它有子目錄,我想遍歷子目錄中的文件
有人可以幫我修改上面的代碼,以便它也包括子目錄。
如果我在Windows中使用下面的代碼的IAM這是在文件夾甚至我的文件夾以外的所有文件的獲取時間戳
my @dirs = ("C:\\Users\\peter\\Desktop\\folder");
my %seen;
while (my $pwd = shift @dirs) {
opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
my @files = readdir(DIR);
closedir(DIR);
#print @files;
foreach my $file (@files) {
if (-d $file and !$seen{$file}) {
$seen{$file} = 1;
push @dirs, "$pwd/$file";
}
next if ($file !~ /\.txt$/i);
my $mtime = (stat("$pwd\$file"))[9];
print "$pwd $file $mtime";
print "\n";
}
}
舊的堅固的方式......沒有遞歸很好地完成=) – Ouki 2012-03-07 11:44:12
'$ file!〜/^\.*$/'是'$ file =〜/[^.]/'。但過去我因爲排除只有三個點或更長的名稱而被嚴厲譴責,因爲它們是Linux文件的有效名稱。所以測試*應該是'$ file!〜/^\.\.?$/' – Borodin 2012-03-07 12:05:53
@perreal如果我想打開文件並在文件中搜索soome特定字符串並分離這些文件,我正在考慮這樣做打開輸入,$文件,然後$行= 然後seraching在它上,它是好的?或者還有其他一些簡單的方法 – Peter 2012-03-07 13:40:33