Perl File::Find in specific order of a list

Is there a way for the File::Find function to find files in a specific order if I were to say, provide an array with the files in order?  If not, is there an alternative approach to File::Find?  I currently am processing directories recursively but I am finding that the more files I add to the search pattern, the more problems I get.  This is due to the order of when the files are found.
mock5cAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

rjmedinaCommented:
Could you provide a bit more information?  
Are you searching for files where you know that they will exist in a few directories or do you really have to recursively search an entire directory?  it seems that this might be easier if you targeted your search.  But this would be more clear to "the experts" if there were more information.  Maybe even a snippet of your current code?
For example, when I am searching for log files, I have a list of known directories to search for log files and then there is an associated regular expression to match the types of files I'm looking for in each directory.
0
wilcoxonCommented:
There is not a good way to have File::Find find files in a specific order.  Processing files/dirs manually is the obvious alternative.

As rjmedina said, we could provide much better assistance given more information.
0
mock5cAuthor Commented:
Attached is code I have that uses File::Find.  Basically, pattern contains the allowable file extensions.  Then within the wanted function, it will check to make sure that the file that was found fits the pattern and if so, it will check for specific file types within the pattern and do something to the files accordingly.  This is done recursively.


use File::Find;

$pattern = '\.xml$|\.csv$|\.zip$|\.bin$';

find(\&wanted, '.');

sub wanted {
   # Process the allowable files
   if (/$pattern/) {

      # Do this if it's an xml or zip file
      if (/\.xml$/ or /\.zip$/) {
         print "processing $File::Find::name\n";
         # do something to file $_
      }
      # Do this if it's a csv file
      if (/\.csv$/){
         print "processing $File::Find::name\n";
         # Do something to a csv file         
      }
   }
}

Open in new window

0
mock5cAuthor Commented:
I should add that for my example above, I would like to make sure that .zip files are processed before other files.  This information can be ordered in an array.
0
wilcoxonCommented:
This should do what you want provided you want to process all zip files in a directory then all others in the same directory then move on to another directory.

If you want to process *all* zip files first, then you'll need to change the below code to do something like store all the filenames of each type in global arrays, pushing the full filenames ("$File::Find::name/$filename") onto the arrays, and the process the files after wanted is finished.
use File::Find;

@priority = ('\.zip$', '\.xml$', '\.$csv', '\.bin');

find(\&wanted, '.');

sub wanted {
   return unless (-d $_); # only look for directories
   my $dir = $_;
   opendir DIR, $dir or die "could not open $File::Find::name: $!";
   my @files = readdir DIR;
   closedir DIR;
   # now get the files we want in the order we want them
   my @to_process;
   foreach my $pri (@priority) {
      push @to_process, grep m{$pri}, @files;
   }
   # now process the files since we have them in the desired order
   foreach my $fil (@to_process) {
      # do whatever with each file
   }
}

Open in new window

0

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
Perl

From novice to tech pro — start learning today.