Perl检查是否以递归方式存在与模式匹配的文件名

时间:2016-11-29 15:54:31

标签: perl

我正在遍历目录中的文件夹,需要检查每个目录中是否存在与模式匹配的文件。我已经使用了glob,但它似乎只适用于第一个文件夹。我找不到第二个文件夹的文件,即使我知道它在那里 这是我的代码:

my @dirs = grep { -d } glob '/data/test_all_runs/*';

for my $dir ( @dirs ) {

    print "the directory is $dir\n";
    my $run_folder = (split '/', $dir)[3];
    print "the folder is $run_folder\n";

    my $matrix_excel =  $dir."/*bcmatrix.xls";
    my $summary_excel = $dir."/*bc_summary.xls";

    unless (-e $summary_excel) {
   if (glob($summary_excel)) {
             At least one file matches "*.file"
            }
            else
            {
        print "File Doesn't Exist!";
        print STDERR "|=============================================|\n";
        print STDERR "|                                             |\n";
        print STDERR "|       Can't find Summary .xls File!!!       |\n";
        print STDERR "|                                             |\n";
        print STDERR "|   Upload the file and rerun the program.    |\n";
        print STDERR "|                                             |\n";
        print STDERR "|=============================================|\n";

                die;
    }
  }  

是否有其他方法可以检查*bcmatrix.xls的每个文件夹中是否存在/data/test_all_runs/*文件?

2 个答案:

答案 0 :(得分:2)

这可能有点矫枉过正,但它似乎做你需要的。我使用File::Find::Rule来获取目录结构中的所有目录,然后使用glob来获取与模式匹配的文件名列表:

鉴于此目录结构:

orig
  |-a
    |-a.txt
  |-b
    |-ba.txt
  |-c

使用此代码:

use warnings;
use strict;

use File::Basename;
use File::Find::Rule;

my $dir = 'orig';
my $file = 'a.txt';

my @dirs = File::Find::Rule->directory
                           ->in($dir);

for (@dirs){
    next if /(?:\.|\.\.)/;

    if (my @files = glob "$_/*$file"){
        for my $path (@files){
            my $name = basename $path;
            print "file $name exists in $_\n";
        }
    }
    else {
        print "file not found in directory $_\n";
    }
}

我得到以下输出:

file not found in directory orig
file ba.txt exists in orig/b
file not found in directory orig/c
file a.txt exists in orig/a

答案 1 :(得分:1)

我建议你使用这样的东西。它将构建一个数组哈希,列出double[][] xyValues = getXYValuesFromData(2.3, 'L'); 的每个子目录中看起来像import java.util.ArrayList; import java.util.Random; public class Test { public static void main(String[] args) { //class for storing my data ScanData myData = new ScanData(); //-----------------------adding data------- //There are 4 possible series(L, R, I, V), I want put them independently, // so e.g. I can put just any one or any two myData.put(2.22, 'L', new XYvalues()); myData.put(2.22, 'R', new XYvalues()); myData.put(2.22, 'I', new XYvalues()); myData.put(2.22, 'V', new XYvalues()); myData.put(2.48, 'I', new XYvalues()); myData.put(3.57, 'I', new XYvalues()); //----------------------getting data-------- //get xyValues of first frequency in my data and R-serie: double[][] xyValuesRserie = myData.freqIndex(0).r(); //get frequency value of first frequency: double freq = myData.freqIndex(0).freq(); //2.22 //get xyValues of l-serie by frequency value double[][] array = myData.freq(5.22).l();//[3000][2] } } class XYvalues{ double[][] XYValues(){ double[][] xyValues = new double[3000][2]; Random random = new Random(); for (int i=0; i< xyValues.length; i++){ xyValues[i][0] = (double) (i - xyValues.length/2)*10; xyValues[i][1] = (double) random.nextDouble(); } return xyValues; } } class ScanData{ //????? } /data/test_all_runs

的所有文件

你应该能够用结果

做你想做的事
*bcmatrix.xls
相关问题