By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,682 IT Pros & Developers. It's quick & easy.

Find::Find

P: n/a
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¬*some thing¬*like¬*this:

enter directory
open INDEX, ".\index.html"
print INDEX HTMLheader
foreach file{
¬*¬*¬*¬*¬*¬*¬*¬*if(file¬*is¬*an¬*image){
¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*process¬*file
¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*print¬*INDEX¬*File Info
¬*¬*¬*¬*¬*¬*¬*¬*}
¬*¬*¬*¬*¬*¬*¬*¬*else¬*if(file¬*is¬*a¬*directory){
¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*¬*print¬*INDEX¬*dire ctorylink
¬*¬*¬*¬*¬*¬*¬*¬*}
}
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¬*ow n¬*functions¬*for¬*processing
subdirectories recursively?
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Dan Jones wrote:
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.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.