Perl 如何遍历目录中的所有文件;如果它有子目录,我也想遍历子目录中的文件

Perl 如何遍历目录中的所有文件;如果它有子目录,我也想遍历子目录中的文件,perl,directory-traversal,Perl,Directory Traversal,基本上,我想记下目录中所有txt文件的时间戳。如果有一个子目录,我也希望在该子目录中包含文件 有人能帮我修改上面的代码,让它也包含子目录吗 如果我在windows中使用下面的代码,iam将获取文件夹中所有文件的时间戳,即使在我的文件夹之外 opendir(DIR,"$pwd") or die "Cannot open $pwd\n"; my @files = readdir(DIR); closedir(DIR); foreach my $file (@files) {

基本上,我想记下目录中所有txt文件的时间戳。如果有一个子目录,我也希望在该子目录中包含文件

有人能帮我修改上面的代码,让它也包含子目录吗

如果我在windows中使用下面的代码,iam将获取文件夹中所有文件的时间戳,即使在我的文件夹之外

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

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

另请参见。

使用

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

最适合于此。它是一个核心模块,因此不需要安装。这段代码与您的想法是一样的

 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";
            }
    }
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";
        }
}
其中,
是要扫描的目录树的根;如果您愿意,可以在此处使用
$pwd
。在子例程中,Perl对找到文件的目录执行了
chdir
$\ucode>设置为文件名,
$file::Find::name
设置为包含路径的完整限定文件名

如果我在windows中使用下面的代码,iam将获取文件夹中所有文件的时间戳,即使在我的文件夹之外

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";
    }
我怀疑问题可能是
目录的问题,如果您尝试跟踪这些目录,它们会使您进入目录树。您缺少的是一个:

use strict;
use warnings;

use File::Find;

find(sub {
  if (-f and /\.txt$/) {
    my $mtime = (stat _)[9];
    print "$mtime\n";
  }
}, '.');
您的脚本还存在一些bug
$file
$dir
不相关,因此
-d$file
只能在当前目录下工作,不能在下面工作

这是我的固定版本:

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

如何区分文件中的文件和目录directory@Peter:使用
-d
-f
操作符,记录
$file!~/^\*$/
$file=~/[^.]/
。但我在过去受到了严厉的谴责,因为我排除了只有三个点或更长的名称,因为它们是Linux文件的有效名称。所以测试应该是
$file!~/^\。\$/
@perreal如果我想打开文件并搜索文件中的某个字符串并将这些文件分开,我想用open INPUT、$file、然后$lines=然后在上面搜索,可以吗?或者还有其他一些简单的方法,您可以使用
grep
。如果你不想使用任何工具,那么按照你的建议去做就可以了。嗨,你们能看到编辑好的问题并帮我解决吗。。我在windows中尝试了该代码,但我也得到了位于我指定文件夹之外的文件夹中的文件的时间戳,我粘贴了有问题的代码。您可以尝试这些更改吗<代码>推送@dirs,“$pwd\\$file”;my$mtime=(stat($pwd\\$file))[9]关于文件::查找是最好的。文件::查找速度很慢,API令人沮丧,但我认为它非常适合这样的琐事。“我想听听有什么人反对它。”我知道这是一条非常古老的评论,但现在链接指向一个登录页面。