JobyJ has asked for the wisdom of the Perl Monks concerning the following question:

I want to be able to process all files that follow the specific filenaming e.g. 2017_06_30_013901_0828900840_17062901D602BG So I want to process only files that contain *****************_0828900840_********D602BG with the help of the below code I was able to process all the files that have "D602BG". How can I achive my additional requirement within the same code.

foreach my $folder (@folders) { # glob the contents of the folder (to get the file names) my @contents = <$folder/*>; # For each filename in the list, if it matches foreach my $item ( @contents ) { if ($item =~ /C602DZB/){ #File name with specific +pattern open my $info, $item or die "Could not ope +n $item: $!"; while( my $line = <$info>) { if ($line =~ /Total/){ #Multiple pa +ttern match done push (@lines, $line); #Matching + lines pushed to an array } } } } }

Replies are listed 'Best First'.
Re: Pick up specific file name
by choroba (Cardinal) on Jul 07, 2017 at 14:22 UTC
Re: Pick up specific file name (updated)
by haukex (Archbishop) on Jul 07, 2017 at 14:20 UTC

    By changing the regular expression (perlretut):

    if ($item =~ /_0828900840_.*D602BG/)

    Or, by using a different glob pattern (Update: However, choroba makes a good point about glob, and I should note that I don't usually use it because of several caveats):

    my @contents = <$folder/*_0828900840_*D602BG>;

    Or by using a module like File::Find::Rule:

    use File::Find::Rule qw/rule/; my @files = rule->file->name(qr/_0828900840_.*D602BG/) ->maxdepth(1)->in(@folders);

    Update: Upon further consideration, I'm withdrawing my suggestion for glob. As soon as $folder contains whitespace, such as a forgotten chomp, it will break in strange ways. Update 2: It's possible to say use File::Glob ':bsd_glob'; to fix that issue, but my position is still "use glob only if you've read all of its documentation, including File::Glob".

Re: Pick up specific file name
by thanos1983 (Parson) on Jul 07, 2017 at 14:23 UTC

    Hello JobyJ,

    Welcome to the monastery. Why don't you try something like this? You can read more for module File::Find::Rule.

    #!usr/bin/perl use strict; use warnings; use File::Find::Rule; sub get_files { my @dirs = ('/path1/path1', '/path2/path2'); # add more my $level = shift // 2; # level to dig into my @files = File::Find::Rule->file() ->name('*D602BG') ->maxdepth($level) ->in(@dirs); return @files; } my @files = get_files();

    After that you can create a foreach loop on the files that you have returned. ;)

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Sure, you can do that. But, in such a simple case, why not simply use glob with the appropriate pattern? Something like this:
      my @files = glob ("$dir/*_0828900840_*D602BG");
      Update: this had already been suggested by choroba, sorry for duplication.
Re: Pick up specific file name
by 1nickt (Canon) on Jul 07, 2017 at 14:25 UTC

    Two regexp tests in one condition:

    next unless $item =~ /_0828900840_/ and $item =~ /D602BG/;


    The way forward always starts with a minimal test.
      thus will match even if the order is inverted, e.g. a name like D602BG*_0828900840_*. Depending on the OP's "real" requirements, this might even be an advantage, but given the specification, it is not.