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 中使用下面的代码,我会获取文件夹中所有文件的时间戳,甚至在我的文件夹之外
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";
}
}