I have a network folder in which i have to search for a file and get the complete path for that particular file. i have tried to implement using recursion.
use dirsearch;
use Cwd;
$dir = "\\\\moon\\builds502\\TEST\\Q1105ASRAWBF100044";
$filename="oncrpc_prot.c";
$path=dirsearch->search($dir,$filename);
print "path of $filename :: $path";
this is the perl script which in turn uses a module which goes like this
package dirsearch;
sub search{
$arg0=shift;
$dir=shift;
$filename=shift;
print $dir,"\n";
print $filename,"\n";
#chdir($dir) or die $!;
chomp($dir);
chomp($filename);
opendir(DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
#Using a regular expression to ignore files beginning with a period
unless ($file =~ m/^\./){
if(-d "$dir\\$file"){
#closedir(DIR);
$dir=$dir.'\\'.$file;
print $dir,"\n";
search($dir,$filename);
}
else{
if($file =~ /$filename/){
$path=$dir."\\".$file;
#print $path,"\n";
return $path;
}
}
}
}
$str="file not found";
return $str;
#closedir(DIR);
}
1;
the network path consists of a system of folders and subfolders..after just 2 runs it reaches to the state of "No such file or directory found in diresearch.pm in line 11" though th开发者_开发知识库e directory exists. the output looks like this
\\moon\builds502\TEST\Q1105ASRAWBF100044
oncrpc_prot.c
\\moon\builds502\TEST\Q1105ASRAWBF100044\Crm
oncrpc_prot.c
No such file or directory at dirsearch.pm line 11.
somebody please help me find out my mistake or anyway to implement my requirement
Instead of calling search($dir,$filename);
within the dirsearch
package, you have to call it as :
$arg0->search($dir,$filename);
In the sub search
you're doing:
$arg0=shift;
$dir=shift;
$filename=shift;
then in the second turn you assign:
$arg0 to $dir
$dir to $filename
and then $filename is undef
so you get No such file or directory ...
And you really must put these two lines at the begining :
use strict;
use warnings;
精彩评论