How to traverse all the files in a directory; if it has subdirectories, I want to traverse files in subdirectories too

点点圈 提交于 2019-12-18 04:47:04

问题


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

Basically I want to note the timestamp of all the txt files in a directory. If there is a subdirectory I want to include files in that subdirectory too.

Can someone help me in modifying the above code so that it includes subdirectories too.

if i am using the code below in windows iam getting timestamps of all files which are in folders even outside my folder

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

回答1:


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



回答2:


File::Find is best for this. It is a core module so doesn't need installing. This code does the equivalent of what you seem to have in mind

use strict;
use warnings;

use File::Find;

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

where '.' is the root of the directory tree to be scanned; you could use $pwd here if you wish. Within the subroutine, Perl has done a chdir to the directory where it found the file, $_ is set to the filename, and $File::Find::name is set to the full-qualified filename including the path.




回答3:


use File::Find::Rule

File::Find::Rule is a friendlier interface to File::Find. It allows you to build rules which specify the desired files and directories.




回答4:


You can use recursion: define a function that goes through files and calls itself on directories. Then call the function on the top directory.

See also File::Find.




回答5:


if i am using the code below in windows iam getting timestamps of all files which are in folders even outside my folder

I suspect that the problem may have been an issue with the . and .. directories which if you tried to follow them would have taken you up the directory tree. What you were missing was a:

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

Your script also suffers from a couple of bugs. $file is not relative to the $dir so -d $file would only work in the current directory and not below.

Here's my fixed version:

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


来源:https://stackoverflow.com/questions/9600395/how-to-traverse-all-the-files-in-a-directory-if-it-has-subdirectories-i-want-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!