1830467 Members
2699 Online
110005 Solutions
New Discussion

Perl 5.6.1 File::Find

 
SOLVED
Go to solution
Steve Labar
Valued Contributor

Perl 5.6.1 File::Find

I have a Perl script which contains a list of files in an array. I need to verify if these files exist in a destination directory (or subdirectory of dest directory). If they do not exist, I need to make a list for later review. I am really new to Perl. perldoc File::Find is not making much sense to me and results of find2perl does not seem to give me results I can use for testing. It doesn't matter to me whether I test it boolean or by value. I am using Perl 5.6.1 on HP-UX 10.20. I have not installed any extra modules. I appreciate any help. Thanks in advance.

Steve
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Perl 5.6.1 File::Find

Hi Steve,

When you are learning, I am a firm beliver in not giving away everything but I will get you started. What you need is the -e (file exists) condition.

I'll assume that your filenames are in an array ,$arry.

This is how to find a file in directory $destdir;

use constant FALSE => 0;
use constanr TRUE => 1;

my $i = 0;
my $fnd = FALSE;

my $destdir = "/dest";
my $fullname = "";
while ($i <= $#arry && !fnd)
{
$fullname = $destdir . "/" . $arry[$i];
$fnd = (-e $fullname);
if (!($fnd))
{
++$i;
}
}
if ($fnd)
{
printf("File %s found\n",$fullname);
}


Now searching subdirectories would require another array but this should get you started.

Regards, Clay
If it ain't broke, I can fix that.
Steve Labar
Valued Contributor

Re: Perl 5.6.1 File::Find

Clay,
Thanks for your reply. I was able to find the files in the "base" directory. I can't search the sub-directories in the same loop? I could setup separate searches for each sub-directory, but I was hoping there was an easier/faster way to get it in one look.


Steve
Rodney Hills
Honored Contributor

Re: Perl 5.6.1 File::Find

Steve,

perl does support recursive function calls. That is one way to handle any tree structured data.

But I believe File::Find has a way to walk subdirectories and maintain its context.

Good Luck

-- Rod Hills
There be dragons...
Ralph Grothe
Honored Contributor

Re: Perl 5.6.1 File::Find

Hi Steve,

attached you may find a very crude example I wrote which may come close to what you want, or at least gets you started.

First, even this scriplet is very tiny you should always use the warning flag (i.e. '-w' in the shebang), or if you use Perl > 5.6.X better the 'use warnings' pragma, and *always* the 'use strict' pragma.
These two save you a lot of headache later when your scripts become larger.

Maybe a word to the use of the File::Find module (which is standard in every current Perl installation and needs not to be installed separately).

The module's find() function takes at least two arguments, of which the first is a subroutine reference (in Perl lingo a sub ref), and all that follows a list of starting paths from where find() should decend in your filesystem tree.

The sub ref is a reference to a so called callback function (you can name it anything you like, only used callback here to make it obvious).
In fact it even could be an anonymous sub ref (which has no name), which would be more suitable in my example, since the sub is very short.
You get a reference to any Perl type by prepending a backslash (this is the easy way, there are others also).
For a sub \& gets the reference.

Now find calls this callback sub each time it gets a file (i.e. Unix file, which is anything) while it decends, and keeps the basename in the special variable $_.
But to make the file tests like for existence (with '-e') we need the full path name, which is stored in $File::Find::name (this is a funny way in Perl to address the variable in a different namespace).
In your callback you do anything which is required (e.g. testing, taking sizes, sorting, parsing etc) for your objective.
Here I just populate the hash %found which gets the basename as key and the pathname as value.

Beware, my matching may fail, because it is very crude (at the shell type "perldoc perlre")

HTH
Ralph
Madness, thy name is system administration
Steve Labar
Valued Contributor

Re: Perl 5.6.1 File::Find

Since I was most concerned with the files not found, Clay's method was more effective even if it was longer. Thank you all for your input.

Steve