Stephen wrote:
>
> Rob Dixon wrote:
> >
> > In essence, what File::Find does is
> >
> > - Read a file directory
> >
> > - If 'postprocess' is specified then the subroutine is called with
> >   the list of files as parameters. The subroutine must then return
> >   a list of those files it is interested in.
> >
> > - The 'wanted' subroutine is called for each member of the list
> >   with the file name as a parameter.
> >
> > - If the 'postprocess' subroutine is specified then it is called
> >   with no parameters.
> >
> > - If any of the files in the list were directories then this
> >   process recurses for each of them.
> >
> > There's a little more to it than that but that's the basics.
>
> I'm sure there's a lot more to it <g>, but I think I get it now.  So
> that I can stop my headscratching, can you provide an example of a
> script where both wanted and postprocess functions are used?

The code below does the same as the previous program, but uses
'preprocess' to sort the files in alpha order before thowing the
list at 'wanted'. You could also remove files from the list here
which will be a little quicker than ignoring them in 'wanted'.

In addition all three routines print indented trace to say why
they've been called. That will show you the general scheme of
things. What you actually want them to do is down to you!

HTH,

Rob


use strict;
use warnings;
use File::Find;

my $file_count = 0;
my $dir_count = 0;
my $indent = 0;

find ( {
  wanted => \&wanted,
  preprocess => \&preprocess,
  postprocess => \&postprocess,
}, 'C:/SomeFolder');

printf "\nThere are %d files in %d directories.\n",
  $file_count,
  $dir_count;

# 'Wanted' callback
#
sub wanted {

  print '  ' x $indent;

  if (-d) {
    return unless /[^.]/;
    print "Directory $File::Find::name\n";
    $dir_count++;
  }
  elsif (-f _) {
    print "File $File::Find::name\n";
    $file_count++;
  }
}

# 'Preprocess' callback
#
sub preprocess {

  print '  ' x $indent;
  print "Processing directory $File::Find::dir\n";

  $indent++;
  sort { uc $a cmp uc $b } @_;
}

# 'Postprocess' callback
#
sub postprocess {

  $indent--;
  print '  ' x $indent;

  print "Finished processing directory $File::Find::dir\n";
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to