Applying a perl script to multiple files

Below you will find a perl script that takes a .mbx file and parses it into multiple separate text files which it stores in a 'all_mail' directory.
Along the way, it searches for specific strings and attempts to eliminate duplicates.  
What I can't figure out how to do is to apply this script to a whole directory of .mbx files.
I'm so frustrated, I have considered gnawing my whole arm off...
I have about 400 .mbx files that I need to run this on... far too many to do manually.
Please help....


#!perl -w

use Fatal qw/open/;
use File::Copy;

$root_dir = 'C:\Files\mbx';
$count = 0;

mkdir("$root_dir\\all_mail") unless -d "$root_dir\\all_mail";

      while (<>) {
            if (/^From (.*) (... ... .. ..:..:.. ....)/) {
                        $filename = sprintf("%04d.txt",$count);
                        open(STDOUT, ">>$root_dir\\all_mail\\$filename");

      @files = glob('*');

      foreach $file (@files) {
               open FILE, "$file";
               @lines = <FILE>;
               foreach (@lines) {      
                   if (/*****************/i) {
                        push(@emails, $file);
               close FILE;

      foreach $file (@emails) {
            open FILE, "$file";
               @lines = <FILE>;
               foreach $line (@lines) {
                  if ($line =~ /^Message-Id/) {
                        ($id) = ($line =~ /(<.+>)/);
                        $ids{$id} = $file;
               close FILE;
      mkdir("$root_dir\\filtered_mail") unless -d "$root_dir\\filtered_mail";

      foreach $value (values %ids) {
               copy("$value", "$root_dir\\filtered_mail");
      #@delete = glob('*');
      #foreach (@delete) {
      #      unlink or print "Cannot unlink : $_ <$!> \n";
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave CrossModule AuthorCommented:
Easiest way is probably to write another program which calls your original script for each file that it finds. Something like this (assuming your program is in


use strict;
use warnings;

my $dir = 'your directory here';

opendir DIR, $dir or die "Can't open $dir: $!n"

foreach (readdir DIR) {
  system(" $dir/$_");
Or you can write a shell script which does the same thing.

for file in *.mbx; do  $file; done
if you use glob in part of your code already, why can't you use

my @mbxfiles = glob("$mbxdir/*.mbx");
foreach my $mbxfile (@mbxfiles)
  # do all your processing here in the loop and of course open each $mbxfile and use that filehandle instead of the null one

am I missing something here?  Why others are suggesting extra scripts for doing it?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.