Hai Raghavan,
I think you aren't aware of the unix command 'find'.
Just try
> find . -file <filename>
Next I've made your program to work, just try this out. I hope
the changes I've made is clear for yor.
#! /usr/local/bin/perl -w
use strict;
my @dirList;
my $fileName;
my $srcDir;
my $findRet;
$fileName = shift || die "Usage : <program_name> <file_name_to_search_for>
<optional_directory_name> : $!";
$dirList[0] = shift || '.';
chomp ($fileName);
chomp ($dirList[0]);
while ($srcDir = shift (@dirList)) {
$findRet = &findFileinDir ($fileName, $srcDir);
if ($findRet == -1) {
print "Cannot open directory $srcDir for reading\n";
}
last if ($findRet == 1);
}
if ($findRet != 1) {
print "File not found\n";
}
sub findFileinDir {
my $srchFile = $_[0];
my $srchDir = $_[1];
my $fileInDir;
opendir DIRHNDL, $srchDir or return -1;
foreach $fileInDir (grep !/^\.\.?$/, readdir DIRHNDL) {
if (-d $srchDir."/".$fileInDir) {
my $subDir = $srchDir;
chomp ($subDir);
$subDir = $subDir."/".$fileInDir;
push (@dirList, $subDir);
}
elsif ($fileInDir eq $srchFile) {
print "$srchDir/$fileInDir\n";
return 1;
}
}
closedir (DIRHNDL);
return 0;
}
--------
I've long back writtern a shell script, think that may help your understanding
# This program searches for a file in pwd & subdirectories
if [ $1 ]
then
file=$1
if [ $2 ]
then
cd $2
fi
for list in *
do
if [ -d $list ]
then
$0 $file `pwd`/$list
else
if [ "`echo $list | grep -i $file`" ]
then
echo `pwd`/$list
fi
fi
done
else
echo "Usage 'dlook filename directoryname'"
fi
------------
Thanks & Regards,
Naren.
"Sudarsan.Raghavan" wrote:
> Hello,
>
> I am new to perl. I want to find for a file recursively within a
> directory. Is there a perl module already present that will do the job
> for me, or do I have to write my own.
> My attempt at the same I am trying this on a VMS machine.
>
> #Begin searchDirforFile.pl
> #Usage perl searchDirforFile.pl <filename> <Directory (optional,
> defaults to current Directory)>
> use strict;
> my @dirList;
> my $fileName;
> my $srcDir;
> my $findRet;
>
> $fileName = shift;
> $dirList[0] = shift || '.';
> chomp ($fileName);
> chomp ($dirList[0]);
>
> while ($srcDir = shift (@dirList)) {
> $findRet = &findFileinDir ($fileName, $srcDir);
> if ($findRet == -1) {
> print "Cannot open directory $srcDir for reading\n";
> }
> last if ($findRet == 1);
> }
>
> if ($findRet != 1) {
> print "File not found\n";
> }
>
> sub findFileinDir {
> my $srchFile = $_[0];
> my $srchDir = $_[1];
> my $fileInDir;
>
> opendir DIRHNDL, $srchDir or return -1;
>
> while ($fileInDir = readdir DIRHNDL) {
> $fileInDir =~ s/\.dir\Z//;
>
> if (-d $srchDir.$fileInDir) {
> my $subDir = $srchDir;
> chop ($subDir);
> $subDir = $subDir.".".$fileInDir."]";
> push (@dirList, $subDir);
> }
> elsif ($fileInDir eq $srchFile) {
> print "$srchDir\n";
> print "$fileInDir\n";
> return 1;
> }
> }
> closedir (DIRHNDL);
> return 0;
> }
>
> Thanks,
> Sudarsan
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]