Perl   发布时间:2019-10-06  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Recursive Find File In Directory大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/usr/bin/perl -w

use utf8;
use Strict;
use warnings;

sub lsrp {
    my ($dir,$sub,$ext,$print) = @_;

    my @dirs = ($dir);

    my @rets = ();

    while (my $dir = pop(@dirs)) {
        local *DH;
        if (!opendir(DH,$dir)) {
            warn "CAnnot opendir $dir: $! $^E";
            next;
        }

        foreach (readdir(DH)) {
            if ($_ eq '.' || $_ eq '..') {
                next;
            }
            my $file = $dir.$_;         
            if (!-l $file && -d _) {
                $file .= '/';
                push(@dirs,$filE) if $sub;
            } elsif ($file =~ /\.\Q$ext\E$/) {
                push @rets,$file;
                $print->($filE) if Defined($print);
            }
        }
        closedir(DH);
    }

    return @rets;
}

my $find_dir = shift @ARGV;
my $find_ext = shift @ARGV;
my $find_sub = shift @ARGV;

if (!defined($find_dir)) {
	Help();
	exit;
}

if (!defined($find_ext)) {
	Help();
	exit;
}

if (!defined($find_sub)) {
	$find_sub = 0;
}

if ($find_dir =~ /[^\/]$/) {
	$find_dir .= '/';
}
$find_ext =~ s/^\.//;

sub p {
	print (shift . "\n");  
}

my @files = lsrp($find_dir,$find_sub,$find_ext,\&p);

if ($#files <= 0) {
	print "Found nothing.\n";
	exit;
}

#print "Found " . $#files . ($#files <= 1 ? " file" : " files") . "\n";
#
#foreach (@files) {
#	print " -> ".$_."\n";
#}

exit;

sub Help {
	print "Usage:\n";
	print "\t$0".' [directroy] [extension] [recursion]'."\n";
	print "Sample:\n\t$0 . pl 1 \n";
}

大佬总结

以上是大佬教程为你收集整理的Recursive Find File In Directory全部内容,希望文章能够帮你解决Recursive Find File In Directory所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: