473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Find::Find

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
1 7571
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
0
by: amit | last post by:
I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE to do it, the find proerty does it, the results are getting...
0
by: AMIT PUROHIT | last post by:
hi, this is a qry which I m stuck up with I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE(EnvDTE) to do it,...
0
by: amit | last post by:
hi I have created a tool which does a find and replace thru DTE, now after it is done, it opens up a window, "FIND REACHED THE STARTING POINT OF SEARCH" I want to disbale this window...
5
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
3
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
0
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.