File :: Find模块如何工作?

时间:2013-08-08 05:37:29

标签: perl

我不想解析我的一些子目录。为此,我可以在下面的这些功能中修改哪些内容。

 use File::Find;
 find(\&wanted, @directories_to_search);
 sub wanted { ... }

这是我的目录树:

LOG
├── a.txt
├── b.txt
└── sdlog
    ├── 1log
    │   ├── a.txt
    │   └── b.txt
    └── 2log
        ├── a.txt
        └── b.txt
    |__abcd
    |__efgh

我想解析 sdlogs 1log 。除了这些子目录,我不想解析任何其他子目录。

1 个答案:

答案 0 :(得分:1)

您不希望File::Find在这里。

查看opendirreaddir

use warnings;
use strict;

# you probably want to use the abs. path
my $dir = "testdir";
opendir(my $dh, $dir);
# grep out directory files from the list of files to work on
# this will also skip "." and "..", obviously :)
my @files = grep { ! -d } readdir $dh;
closedir $dh;

# change to the given directory, as readdir doesn't return the relative path
# to @files. If you don't want to chdir, you can prepend the $dir to $file as 
# you operate on the $file
chdir $dir;
for my $file (@files) {
    # do stuff.. 
    # E.g., "open my $fh, ">>", $file;", etc

    print $file, "\n";
}

输出

$ ./test.pl
a_file.txt
b_file.txt
c_file.txt