Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 在目录结构中搜索文件_Perl_Search - Fatal编程技术网

Perl 在目录结构中搜索文件

Perl 在目录结构中搜索文件,perl,search,Perl,Search,有人知道不使用file::Find在目录结构中搜索文件的方法吗?我知道如何一步一步地做,但如果有可能使它变得更平滑,那将非常有用。File::Find是perl 5.000以来的一个核心模块,因此我不认为有理由不使用它 但是,如果您仍然想采取疯狂的方式,可以调用find命令。更为平滑 use File::Find::Rule qw( ); say for File::Fine::Rule->in("."); 从一个文件::Find hater到另一个文件:DirWalk.pm,灵感来自P

有人知道不使用file::Find在目录结构中搜索文件的方法吗?我知道如何一步一步地做,但如果有可能使它变得更平滑,那将非常有用。

File::Find是perl 5.000以来的一个核心模块,因此我不认为有理由不使用它

但是,如果您仍然想采取疯狂的方式,可以调用find命令。

更为平滑

use File::Find::Rule qw( );
say for File::Fine::Rule->in(".");
从一个文件::Find hater到另一个文件:DirWalk.pm,灵感来自Python的os.walk

例如:

# prepare test structure
mkdir aaa
touch aaa/bbb
mkdir aaa/ccc
touch aaa/ccc/ddd

# example invocation:
perl -mDirWalk -E '$dw=DirWalk->new("aaa"); say while <$dw>;'

#output
aaa/ccc/
aaa/ccc/ddd
aaa/bbb
另一个例子:

use strict;
use warnings;
use DirWalk;

# iteration:
my $dw = DirWalk->new("aaa");
while (<$dw>) {
    print "$_\n";
}

# or as a list:
$dw = DirWalk->new("aaa");
my @list = <$dw>;
for (@list) {
    print "$_\n";
}

我所描述的方法是使用三个命令:opendir、readdir和closedir。请参见下面的示例:

opendir my $dir1, $cwd or die "cannot read the directory $cwd: $!";
@cwd= readdir $dir1;
closedir $dir1;
shift @cwd; shift @cwd;
foreach(@cwd){if ($_=~/$file_search_name/){print "I have found the file in $_\n!";}}
该目录将存储在@cwd中,其中包括。和对于windows,shift@cwd将删除这些。不幸的是,我的时间很紧,但利用anon数组存储目录句柄以及另一个存储目录路径的数组的这种想法。也许可以利用-d检查它是否是一个目录。可能存在文件权限问题,因此可能unlessopendir。。。这将是一个很好的选择


祝你好运。

我肯定我会被活剥这个答案,但你可以使用system或backticks``来执行常规的linux find命令。或者做一些类似的事情

@files = `ls $var/folder/*.logfile`
@files = `find . -name $file2find`

我想一些经验丰富的Perler有很多理由不这样做。

你也可以试试这样的东西

# I want to find file xyz.txt in $dir (say C:\sandbox)

    Findfile("xyz.txt", $dir);

    sub Findfile ()
    {
        my $file = shift;
        my $Searchdir = shift;

        my @content = <$Searchdir/*>; 

        foreach my $element (@content)
        {
             if($element =~ /.*$file$/)
             {
                print "found";
                last;
             }
             elsif (-d $element)
             {
                 Findfile($file, $element); #recursive search
             }
        }

    }

可能重复文件::Find有什么问题?!有多种方法,但出于各种原因,默认内置可能是最好的。或者是文件::查找::规则。目录遍历有很多问题,如果使用模块,您不必担心。问题是我无法安装任何模块。SECURITY@PopescuEmanuel文件::查找是一个核心模块。不需要安装任何东西。与前面提到的文件::Find::Rule相比,它有什么优势?@PopescuEmanuel:因此,您不能使用Perl安装中的核心模块,也不能使用CPAN中经过尝试和测试的模块,但可以从internet上的任意位置复制和使用未知且未经测试的模块?这很奇怪security@ikegami,如果你问我的话。File::Find::Rule是一个非常混乱的东西,默认情况下不会安装它。而我的WalkDir是一个小东西,如果需要,我可以适应。另外,WalkDir实际上会在遍历树时遍历它:它不会像File::Find::Rule那样将所有内容都包含在列表中。但就我个人而言,我并不反对File::Find::Rule,只是当我需要它时,它不在那里。总而言之,你说FFR比WalkDir使用更多的内存。@ikegami,我在看过Python的os.walk后编写了WalkDir-与Perl的File::Find的无可救药的缺点相比,它是多么简单和优雅。没有大的想法/等参与-绝对没有冗长的分析和比较可用的CPAN模块-我只是想要一些简单的东西来迭代子目录/文件。欢迎使用StackOverflow,在这样做之前您可能需要阅读。请给出一点上下文,解释为什么要发布该代码以及它的作用。也要考虑花时间格式化你的答案以便于读者理解。
# I want to find file xyz.txt in $dir (say C:\sandbox)

    Findfile("xyz.txt", $dir);

    sub Findfile ()
    {
        my $file = shift;
        my $Searchdir = shift;

        my @content = <$Searchdir/*>; 

        foreach my $element (@content)
        {
             if($element =~ /.*$file$/)
             {
                print "found";
                last;
             }
             elsif (-d $element)
             {
                 Findfile($file, $element); #recursive search
             }
        }

    }