在目录结构中搜索文件

时间:2016-04-07 11:28:30

标签: perl search

有人知道在不使用angularBlogServices.factory('BlogPost', ['$resource', function($resource) { return $resource("./Post/:id", {}, { get: {method: 'GET', cache: true, isArray: false}, save: {method: 'POST', cache: false, isArray: false}, update: {method: 'PUT', cache: false, isArray: false}, delete: {method: 'DELETE', cache: false, isArray: false} }); }]); 的情况下在目录结构中搜索文件的方法吗?我逐步知道如何做到这一点,但如果可以使它更顺畅,那将会有所帮助。

6 个答案:

答案 0 :(得分:2)

File :: Find是一个核心模块,因为perl 5.000所以我没有看到不使用它的原因。

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

答案 1 :(得分:1)

从一个File::Find仇恨到另一个:DirWalk.pm,受到Python os.walk()的启发。

package DirWalk;

use strict;
use warnings;

sub new {
    my ($class, @dirs) = @_;

    my @odirs = @dirs;

    @dirs = qw/./ unless @dirs;
    s!/+$!! for @dirs;
    s!/+\.$!! for @dirs;

    my $self = { _odirs => [@odirs], _dirs => [@dirs], _dhstack => [], _dnstack => [] };

    opendir my($dirh), $dirs[0];
    return undef unless $dirh;
    shift @{ $self->{_dirs} };
    unshift @{ $self->{_dhstack} }, $dirh;
    unshift @{ $self->{_dnstack} }, $dirs[0];

    return bless $self, $class;
}

sub _walk_op {
    my ($self) = @_;
    if (wantarray) {
        my @ret;
        while (defined(my $x = $self->next())) {
            push @ret, $x;
        }
        return @ret;
    }
    elsif (defined wantarray) {
        return $self->next();
    }
    return undef;
}

sub next
{
    my ($self) = @_;
    my $dstack = $self->{_dhstack};
    my $nstack = $self->{_dnstack};
    if (@$dstack) {
        my $x;
        do {
            $x = readdir $dstack->[0];
        } while (defined($x) && ($x eq '.' || $x eq '..'));

        if (defined $x) {
            my $nm = $nstack->[0].'/'.$x;
            if (-d $nm) {
                # open dir, and put the handle on the stack
                opendir my($dh), $nm;
                if (defined $dh) {
                    unshift @{ $self->{_dhstack} }, $dh;
                    unshift @{ $self->{_dnstack} }, $nm;
                }
                else {
                    warn "can't walk into $nm!"
                }
                $nm .= '/';
            }
            # return the name
            return $nm;
        }
        else {
            closedir $dstack->[0];
            shift @$dstack;
            shift @$nstack;
            unless (@$dstack) {
                while (@{ $self->{_dirs} }) {
                    my $dir = shift @{ $self->{_dirs} };
                    opendir my($dirh), $dir;
                    next unless defined $dirh;
                    unshift @{ $self->{_dhstack} }, $dirh;
                    unshift @{ $self->{_dnstack} }, $dir;
                    last;
                }
            }
            return $self->next();
        }
    }
    else {
        return undef;
    }
}

use overload '<>' => \&_walk_op;
use overload '""' => sub { 'DirWalk('.join(', ', @{$_[0]->{_odirs}}).')'; };

1;

示例:

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

答案 2 :(得分:0)

我一直在使用的方法是使用三个命令: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,转移@cwd将删除这些。遗憾的是我时间紧迫,但利用这个想法与anon数组存储目录句柄以及另一个数组来存储目录路径。也许利用-d来检查它是否是一个目录。可能存在文件权限问题,因此可能除非(opendir ...)是一个很好的选择。

祝你好运。

答案 3 :(得分:0)

我确信我会为这个答案活着,但你总是可以使用system()或反引号来执行常规的linux find命令。或者做某种事情......

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

我希望一些经验丰富的演员有很多理由不这样做。

答案 4 :(得分:0)

你也可以试试这样的东西!!!

# 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
             }
        }

    }

答案 5 :(得分:-1)

File::Find::Rule&#34;更顺畅&#34;。

use File::Find::Rule qw( );
say for File::Fine::Rule->in(".");
相关问题