Find::Find

D

Dan Jones

I'm writing a script to process a directory tree of images.  In each
directory, I need to process each image and generate an HTML file listing
all of the images and links to the subdirectories.

Just about every source I can find on the 'net for processing subdirectories
points you at Find::Find.  However, I'm trying to do something like this:

enter directory
open INDEX, ".\index.html"
print INDEX HTMLheader
foreach file{
        if(file is an image){
                process file
                print INDEX FileInfo
        }
        else if(file is a directory){
                print INDEX directorylink
        }
}
print INDEX HTMLfooter
close INDEX
next directory

I need to be able to deal with a full file tree - multiple subdirectories
several levels deep.

As near as I can tell, Find::Find was not designed for this.  It's only
capable of running a command on each file, not processing files and file
names in batches as I'm trying to do.  Am I missing some of Find::Finds
capabilities?  Or do I need to roll my own functions for processing
subdirectories recursively?
 
J

Joe Smith

Dan said:
As near as I can tell, Find::Find was not designed for this. It's only
capable of running a command on each file, not processing files and file
names in batches as I'm trying to do. Am I missing some of Find::Finds
capabilities?

Yes, you're missing the 'preprocess' and 'postprocess' hooks that I added
to File::Find back in 2000.

Here's how to get all the file names and then process as a single batch:

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use vars qw/*name/;
*name = *File::Find::name;
our(@files,@dirs);
find( sub {if(-d $_){push @dirs,$name}else{push @files,$name}}, 'Olympus');
print "Directories: @dirs\nFiles: @files\n";



Here's how to process the beginning and the end of directories, recursively.

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use vars qw/*name *dir/;
*name = *File::Find::name;
*dir = *File::Find::dir;

my %options = (
wanted => \&wanted,
preprocess => \&pre,
postprocess => \&post,
);

our %info;
find \%options, qw(Olympus Canon); # Subdirectories full of pictures
print "$info{$_}\n\n" for sort keys %info;
exit;

sub pre { # Called before entering a directory
$info{$dir} = "\t DIR: $dir\n";
sort @_; # Pre-process directory entries by sorting them
}

sub wanted { # Called for each directory entry
$info{$dir} .= (-d $_) ? "Dir: $_\n" : "File: $_\n";
}

sub post { # Post-process dir by appending END
$info{$dir} .= "\t END: $dir\n";
}


-Joe

P.S. Don't post to comp.lang.perl; post to comp.lang.perl.misc instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top