Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Find::Find

Reply
Thread Tools

Find::Find

 
 
Dan Jones
Guest
Posts: n/a
 
      10-07-2004
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?
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      10-08-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57