• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

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.
0
mock5c
Asked:
mock5c
  • 2
  • 2
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now