11
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";
            }
    }
4

5 回答 5

18

File::Find最适合这个。它是一个核心模块,所以不需要安装。这段代码的作用与您的想法相同

use strict;
use warnings;

use File::Find;

find(sub {
  if (-f and /\.txt$/) {
    my $mtime = (stat _)[9];
    print "$mtime\n";
  }
}, '.');

'.'要扫描的目录树的根在哪里;$pwd如果你愿意,你可以在这里使用。在子例程中,Perl 对chdir找到文件的目录进行了操作,$_设置为文件名,并$File::Find::name设置为包含路径的全限定文件名。

于 2012-03-07T11:52:35.587 回答
7
use warnings;
use strict;

my @dirs = (".");
my %seen;
while (my $pwd = shift @dirs) {
        opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
        my @files = readdir(DIR);
        closedir(DIR);
        foreach my $file (@files) {
                next if $file =~ /^\.\.?$/;
                my $path = "$pwd/$file";
                if (-d $path) {
                        next if $seen{$path};
                        $seen{$path} = 1;
                        push @dirs, $path;
                }
                next if ($path !~ /\.txt$/i);
                my $mtime = (stat($path))[9];
                print "$path $mtime\n";
        }
}
于 2012-03-07T11:39:38.303 回答
4

使用File::Find::Rule

File::Find::Rule 是 File::Find 的一个更友好的界面。它允许您构建指定所需文件和目录的规则。

于 2012-03-07T11:26:27.093 回答
1

您可以使用递归:定义一个遍历文件并在目录上调用自身的函数。然后调用顶层目录中的函数。

另请参阅文件::查找

于 2012-03-07T11:18:37.130 回答
0

如果我在 Windows 中使用下面的代码,我会获取文件夹中所有文件的时间戳,甚至在我的文件夹之外

我怀疑问题可能是.and..目录的问题,如果您尝试遵循它们,则会将您带到目录树中。您缺少的是:

foreach my $file (@files) {
    # skip . and .. which cause recursion and moving up the tree
    next if $file =~ /^\.\.?$/;
    ...

您的脚本还存在一些错误。 $file不相对于,$dir所以-d $file只能在当前目录而不是下面的目录中工作。

这是我的固定版本:

use warnings;
use strict;

# if unix, change to "/";
my $FILE_PATH_SLASH = "\\";    
my @dirs = (".");
my %seen;
while (my $dir = shift @dirs) {
        opendir(DIR, $dir) or die "Cannot open $dir\n";
        my @files = readdir(DIR);
        closedir(DIR);
        foreach my $file (@files) {
                # skip . and ..
                next if $file =~ /^\.\.?$/;
                my $path = "$dir$FILE_PATH_SLASH$file";
                if (-d $path) {
                        next if $seen{$path};
                        $seen{$path} = 1;
                        push @dirs, $path;
                }
                next unless $path ~= /\.txt$/i;
                my $mtime = (stat($path))[9];
                print "$path $mtime\n";
        }
}
于 2018-05-07T18:38:53.460 回答