If you have any experience on UNIX, you've certainly found a need for the find command, which is useful to search for filenames throughout the file system. In particular, you can use wildcards to match filenames and recursively traverse any directory structure (where permissions allow). The UNIX find command can also execute other commands on the files it finds.
The File::Find module within Perl encompasses the same functionality and also gives you the advantage of programmatic structures. To show how this works, I'll walk you through a sample script that employs the File::Find module.
A simple exampleThis simple Perl script can help you clean up your PC hard drive by finding any files that end with .tmp, .chk, or .zip or that begin with the ~ symbol. (You can see the entire script in Listing A.) The script will print the full path of each file it finds, and a tally of the number of bytes being consumed will appear at the end. You can run the script on either Windows or UNIX if Perl has been installed. Note that in a UNIX environment, you must modify the first line of the script: Change the /bin/perl path to match the path to Perl in your environment. For this example, I will assume that you are running it in a Windows environment. The solution
Of course, Microsoft's GUI Find utility offers a portion of this functionality. But I wrote the script because once you have the file within Perl, you can do all sorts of things with it, such as open it up and look for a particular pattern, automatically delete it, or use it as input for another application. I make use of one module within the standard Perl Library and one Perl function, so all of the necessary modules should be available when you install Perl on your Windows machine. (I grabbed Perl from ActiveState.) The File::Find function mimics the UNIX find command and will traverse a file tree. Here's the API for the method: Find(\&yoursubroutine, ‘dir1', ‘dir2'…); You provide the subroutine, which I will detail later, and a list of directories where you want the search to be conducted. Remember that these directories will be traversed in a depth-first fashion. The other method I use is the stat() function (similar to the C library function of the same name), which gives you all sorts of information about the filename it takes as an argument. Listing B shows the API for the method. Notice that the function returns the values in a list. The only value we're interested in is $size, which contains the size in bytes of the file given. All of the work will be performed for the utility in the subroutine. Remember that it will be called each time a file is encountered, so it's our job to determine whether the filename matches the files we're looking for. The File::Find method has special variables available that will be populated with certain information, as shown here:
- $_ contains the current filename within the directory
- $File::Find::dir contains the current directory name
- $File::Find::name contains $File::Find::dir/$_
Table A
|
Tech Update forum. Find out what's where in the new Tech Update with our
Guided Tour. Let the editors know what you think in the
Mailroom.






