Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Marshall's scratchpad

by Marshall (Abbot)
on Dec 26, 2008 at 13:00 UTC ( #732670=scratchpad: print w/replies, xml ) Need Help??

Using File::Find

This tutorial shows how to use the File::Find module.

File::Find is a core module, meaning that it is already installed on all Perl systems - no installation is required! If the system has Perl, File::Find is already there!

The find() function is included within that File::Find module. The first step is to tell Perl that you want to use this pre-installed module - that will make the subroutine "find()" known to the rest of the code.

Getting Started:

You need to include a use File::Find; statement at the top of the code, this "imports" the "find" fuction and makes it known to the rest of the code.

What the find() routine does...

The man (manual) page - shown by (perldoc file::find) or via Google search says:

use File::Find;
find(\&wanted, @directories_to_search);
sub wanted { ... }

I really don't why this "wanted" name got used, but that is a reference to a subroutine name, followed by a list of directory names.

The find() function will cwd (change working directory) into the the first directory in that list and do a readdir(). It will then call the "wanted()" function for every single name from that readdir() list. It might be a directory, it might be a simple file, it might be some kind of linked file/directory (not ususal, but possible).

After find() does that, it will follow the directory names, meaning that it will cwd (change working directory) into the next directory name and continue following it downward calling the "wanted()" function on every "name" whether it be a directory or a simple file or some kind of link.

If we have:

use File::Find; find (\&for_every_name, (C:/temp, C:/Windows); my @allFiles; sub for_every_name { print "$File::Find::name\n"; # note there is no "return value" # it is not possible to return anything from this # subroutine # If you want that: push @allFiles, $File::Find::name; # you must save information in a varialble that has higher # scope than this subroutine # There is NO return value from this subroutine! }
The above will print the names of all files (and directories and symbolic links) underneath C:/temp and C:/WINDOWS (NOTE: Windows file systems are case insensitive, but Unix file systems are case sensitive - so be aware of that!).

Special Variables

Within the subroutine that the find() routine calls, a couple of special variables to tell you where it is "at":

-$File::Find::name - the name of the file that the "wanted()" function is currently looking at.

-$File::Find:dir - the name of the current directoryt that the wanted() function is currently within.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2017-05-28 02:15 GMT
Find Nodes?
    Voting Booth?