多进程遍历目录并查找文件

有时候需要在一个深层次的目录下面查找某个类型的文件,这里利用遍历递归查找目录,并使用多进程操作提高效率。

如果现在只需要在一个目录下查找,脚本getfile_single.pl为:

use strict;
my @ARGV==2||die"usage:*.pl dir rec\n";
my $dir=$ARGV[0];##input directory
my $logfile=$ARGV[1];##result file to save path
my @all;
my $str;
my $grep = "";###this is the regular exression to match the file expected
open(FILE,">$logfile")||die"can't write the file:$!\n";
::ErgodicDirToGetFile($dir);
close(FILE);

sub ::ErgodicDirToGetFile
{
    my ($dir) = @_;
    if(-d $dir)
    {
        opendir(DIRHANDLE,$dir);
        my @dirs = grep(!/^\.\.?$/,readdir(DIRHANDLE));##delete the element "." and ".."
        closedir(DIRHANDLE);
    }
    
    foreach my $str(@dirs)
    {
        if(-d "$dir\\$str")
        {
            ::ErgodicDirToGetFile("$dir\\$str");
        }
        else
        {
            my $temp = "$dir\\$str";
            if($temp =~/$grep/)
            {
                print FILE "$temp\n";
            }
        }
    }    
}

 

如果有多个目录,或许多进程可以提升查找效率:

use strict;
my @all;
my $str;
my @pid;
my $i;
open(FILE,"dir.list")||die"can't open the file:$!\n";##dir.list is the file include all the directory you want to search for your desire file
@all=<FILE>;
chomp(@all);
close(FILE);

for($i=0; $i<@all; $i++)
{
    $str = $all[$i];
    defined($pid[$i]=fork())||die"can't fork\n";
    if($pid[$i]==0)##sub process begin
    {
        my $cmd = "perl getfilepath_single.pl $str result_$i.scp";##result_$i.scp is the result file to save path
        print "$cmd\n";
        system($cmd);
        exit(0);
    }
}
for($i=0; $i<@all; $i++)
{
    waitpid($pid[$i],0);
}

 

转载于:https://www.cnblogs.com/wb-DarkHorse/archive/2012/10/15/2724269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值