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]

Reply via email to