如何遍历一个目录下的所有文件;如果有子目录,我也想遍历子目录中的文件

问题描述 投票:0回答:6
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";
            }
    }
perl directory-traversal
6个回答
19
投票

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
设置为包括路径的完全限定文件名。


8
投票
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";
        }
}

4
投票

使用文件::查找::规则

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


1
投票

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

另请参阅文件::查找


0
投票

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

我怀疑问题可能是

.
..
目录的问题,如果您尝试遵循它们,则会将您带到目录树中。 你缺少的是: 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"; } }



0
投票

创建一个名为 Amazing_perl.pl 的 Perl 脚本并将其添加到其中

然后像这样运行>

perl.exe Amazing_perl.pl c: emp

#!/usr/bin/perl use strict; use warnings; use File::Find qw(finddepth); my ($TOP_LEVEL_DIR) = @ARGV; print "Perl Starting ...\n\n"; #create output file open(my $output_file_handle, '>', "ALL_FILES_AND_FOLDERS.txt"); print "Processing ...\n"; finddepth(sub { return if($_ eq '.' || $_ eq '..'); return if (-d $_); print "Processing ... $_ \n"; my $filePath = $File::Find::name; #print "filePath ... $filePath \n"; print $output_file_handle "$filePath\n"; }, "$TOP_LEVEL_DIR"); print "\n\nPerl End ...\n\n"; sub trim { (my $s = $_[0]) =~ s/^\s+|\s+$//g; return $s; } 1 ;

	
© www.soinside.com 2019 - 2024. All rights reserved.