Hi Adriano,

Here is a start.  You might have to adapt it somewhat for youe specific uses.  It 
originated from a proplem Paul posed concernig a search for files of a given name on a 
single level of a directory tree, and greww from there.  It's taken on a bit of random 
testion g code also.  The indent function was born with this program, but never used 
in it.  It has since been moved to a *.pm file.

Have Fun,

Joseph

#!/usr/bin/perl
use warnings;
#use strict;

# Utility to search filenames and text in a given folder and all subdirectoies
# Usage: DirTreeTest.pl -f|-t $baseDir $soughtFile
# If the first parameter is -f, the utility returns all folders containg the given 
file.
# If the first parameter is -t, the utility returns all files containg the given text.


my $option = $ARGV[0];
my $baseDir  = $ARGV[1];
my $soughtFile = $ARGV[2];
my $indentSize = 2;
if ($option eq "-f") {
  ListDirectoriesContainingFile ($ARGV[1], $ARGV[2], 0);
} elsif ($option eq "-t") {
  ListFilesContainingText ($ARGV[1], $ARGV[2], 0);
} else {
  die "You must specify either -f or -t";
}

sub ListDirectoriesContainingFile {
  my $currentDir  = $_[0];
  my $targetFile = $_[1];
  my $indentation = $_[2];
  my $DIR;
  opendir $DIR, $currentDir or die "Cannot opendir $currentDir: $!";
  my $testFile = "";
  while (my $file = readdir $DIR )  {
    $testFile = "$currentDir\\$file";
    if ( -d $testFile) {
      my $go;
      if ($go = ($file ne "." and $file ne "..")) {
        if (-e "$testFile\\$targetFile") {
          print "Found in:\n---=>>$testFile\n";
        }
        ListDirectoriesContainingFile("$testFile",
         $targetFile, $indentation + 2);
      }
    }
  }
}

sub ListFilesContainingText {
  my $currentDir  = $_[0];
  my $targetText = $_[1];
  my $indentation = $_[2];
  chomp $targetText;
  my $DIR;
  print "current directory is $currentDir\n";
  opendir $DIR, $currentDir or die "Cannot opendir $currentDir: ";
  my $testFile = "";
  my $file = "";
  while (defined($file = readdir $DIR) and $file ne "") {
    $testFile = "$currentDir\\$file";
    if ( $testFile and (-d $testFile)) {
      if ($file eq "\." || $file eq "\.\.") {;
      } else {
        ListFilesContainingText($testFile, $targetText, ($indentation + 2));
      }
    }
    else {
      #print "file $testFile checked\n";
      if ($file =~ /\..*\./) {
        print " ~~~> $testFile is ugly in form\n";
      }
      open (SEEK_FILE, $testFile) or print "can not open $testFile\n";
      my $lineNumber = 1;
      my $CurrentLine = <SEEK_FILE>;
      my $IsIt;
      while ($CurrentLine) {
        if ($IsIt = ($CurrentLine =~ /\b$targetText\b/)) {
          print "--=>> $testFile: $lineNumber \n";
          print " |$CurrentLine \n";
          #continue;
        }
        $lineNumber++;
        $CurrentLine = <SEEK_FILE>;
      }
      close SEEK_FILE;
    }
  }
}

sub indent {
  my ($indentation, $textString) = @_;
  my $indent = "";
  $oneSpace = " ";
  for ($i = 0; $i < $indentation; $i++) {
    $indent = $indent.$oneSpace;
  }
  print "$indent$textString"; print "\n";
}


Adriano Allora wrote:

> I need to know, in the @ARGV, when a string is the name of a directory.
> More precisely I'd like to write a script that:
>
> processes the files of the textes in a dir
> extracts the list of the sub-directories in a dir
> in each sub-dir processes the files of the textes
>                                 extracts the list of the sub-directories
>                                 in each sub-dir processes the files of the textes
>                                                                 extracts the list of 
>the sub-directories
>                                                                         and go on...
>
> Some advices?
>
> all'adr
>
> --
> 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]

Reply via email to