Recursive Directory Traversal

S

Sam

I'm trying to search a directory tree for a specific file, when that file is
found I need the path returned.

Does anyone know how to do this?
I have been trying to use File::Find. It traverses the directory tree well
enough, but it does not return anything.

In this specific example, I can assume that the file name I'm looking for
only occurs once.

Does anyone know how to do this? It seems as though this should be a
straightforward problem.

Thanks
 
P

Paul Lalli

Sam said:
I'm trying to search a directory tree for a specific file, when that file is
found I need the path returned.

Does anyone know how to do this?
I have been trying to use File::Find. It traverses the directory tree well
enough, but it does not return anything.

No it doesn't. Nor is it supposed to. The point of File::Find is to
give you a *generic* recursive directory traversal. You pass it a call
back subroutine to do whatever it is you actually want to do. In this
case, you want to set a path:

use File::Find;
my $path;
find ($directory_to_search, sub { $path = $File::Find::name if $_ eq
'specific.file' };
print "Path found: $path\n";
In this specific example, I can assume that the file name I'm looking for
only occurs once.

Does anyone know how to do this? It seems as though this should be a
straightforward problem.

It is. But you have to solve the problem. When you use File::Find,
you tell find() exactly what you want to do for every file and
directory it finds. All it does is move through the directory tree for
you.

If you don't like that, you may be interested in one of two CPAN
modules: File::Find::Rule, or File::Finder. Each have their merits. .
..

Paul Lalli
 
A

A. Sinan Unur

Sam said:
I'm trying to search a directory tree for a specific file, when that
file is found I need the path returned.

Does anyone know how to do this?
I have been trying to use File::Find. It traverses the directory tree
well enough, but it does not return anything.


Please post a short but complete script so we can help you figure out
what you did wrong, and how to achieve the desired result.
In this specific example, I can assume that the file name I'm looking
for only occurs once.

Does anyone know how to do this? It seems as though this should be a
straightforward problem.

use File::Finder;

my ($file) = File::Finder->in('/path/filename.ext');

Sinan
 
P

Paul Lalli

Paul said:
use File::Find;
my $path;
find ($directory_to_search, sub { $path = $File::Find::name if $_ eq
'specific.file' };

Forgot the closing ). Sorry.

Paul Lalli
 
S

Sam

It looks like file::finder is what I want to do the job.
I'll give that a try.
Thanks everyone!
 
R

Randal L. Schwartz

Paul> Forgot the closing ). Sorry.

And got the parameters in reverse order.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
P

Paul Lalli

Randal said:
Paul> Forgot the closing ). Sorry.

And got the parameters in reverse order.

D'oh! I am *not* doing well today. Apologies to the OP, and thanks to
Randal for the correction...

Paul
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top