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 open $item: $!"; while( my $line = <$info>) { if ($line =~ /Total/){ #Multiple pattern match done push (@lines, $line); #Matching lines pushed to an array } } } } }