recursive perl algorithm 
Author Message
 recursive perl algorithm

Hallo all , below is a scripts that would go into a directory , search for
files with a target string and write to a log file the line that the string
occurs.
Can anyone help me to make it a recursive algorithm such that it would go
into the directory and execute the process from the parent into the
sub-directories downwards like a tree ??
Thanks !!

##########################################################
#!/usr/bin/perl

$SCRIPTSPATH='/home/usr//cgi-bin/Scripts'; #path of this script
$SAVEPATH='/home/usr/cgi-bin/Scripts/data'; # path of the log
$SEARCHPATH='/home/usr/cgi-bin/other/Search'; #path of the searched dir
$TARGETSTR="/opt/pkgs/release/cgi-bin"; # the target string

opendir(SEARCHDIR,"$SEARCHPATH") || die "Cannot open SEARCHDIR file";

while( $_=readdir(SEARCHDIR) ){

     $presentfile=$_;

     if(-d "$SEARCHPATH/$_" ){

      mkdir("$SAVEPATH/$_", 0777);
      chmod(0777 , "$SAVEPATH/$_");

 }elsif(-f "$SEARCHPATH/$_" ){
      open(SAVEFILE,">>$SAVEPATH/$_");
      chmod(0777 , "$SAVEPATH/$_");
        open(SEARCH,"$SEARCHPATH/$_");
       while(<SEARCH>){

            if($_=~ /$TARGETSTR/){

                             print SAVEFILE $.;
                             print SAVEFILE "\n";

            }
          }

  close SEARCH;
 }

 close(SAVEFILE);

Quote:
}

open(LOG,">>$SCRIPTSPATH/log");
opendir(SAVEDIR,"$SAVEPATH") || die "Cannot open SAVEDIR file";

while( $_=readdir(SAVEDIR) ){

 if( (-f "$SAVEPATH/$_")  && !(-z "$SAVEPATH/$_") ){

  print LOG "****Files in $SEARCHPATH/$_ *******";
  open(DESFILE,"$SAVEPATH/$_");

   print LOG "$_\n";
   while(<DESFILE>){

    print LOG $_;

   }

  print LOG "*************\n\n";

  close DESFILE;

 }

Quote:
}

system("rm $SAVEPATH\/* ");
system("rmdir $SAVEPATH\/* ");
closedir SAVEDIR;
close LOG;

exit;



Sat, 01 Dec 2001 03:00:00 GMT  
 recursive perl algorithm

Quote:

> Hallo all , below is a scripts that would go into a directory , search for
> files with a target string and write to a log file the line that the string
> occurs.
> Can anyone help me to make it a recursive algorithm such that it would go
> into the directory and execute the process from the parent into the
> sub-directories downwards like a tree ??

I would recommend using the module File::Find ....

/J\
--
"If they want a circus, they have come to the right person" - Neil
Hamilton



Sat, 01 Dec 2001 03:00:00 GMT  
 recursive perl algorithm

Quote:

>Hallo all , below is a scripts that would go into a directory , search for
>files with a target string and write to a log file the line that the string
>occurs.
>Can anyone help me to make it a recursive algorithm such that it would go
>into the directory and execute the process from the parent into the
>sub-directories downwards like a tree ??

File::Find is your friend. Comes standard with the Perl distribution.

Quote:
>#!/usr/bin/perl

#!/usr/bin/perl -w

use strict;

Quote:
>opendir(SEARCHDIR,"$SEARCHPATH") || die "Cannot open SEARCHDIR file";

It's good that you check the return value of the system call, but you
might also want to output the error message:

opendir(SEARCHDIR,"$SEARCHPATH") || die "Cannot open $SEARCHPATH
directory: $!\n";

HTH

Marcel



Sat, 01 Dec 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. recursive opendir(),readdir....efficient algorithm?

2. Newbie: Recursive data structure with recursive subroutine.

3. Recursive subroutine output to recursive subroutine problem

4. Pattern matching algorithm in Perl

5. perl algorithm..

6. Rounding algorithms in PERL??

7. Implementing a PERL algorithm in C. Experience anyone?

8. perl algorithm

9. Opinion on 'Mastering Algorithms with Perl'

10. Search algorithm in Perl

11. Perl hashing algorithm description?

12. Algorithms: Cluster analysis w/ Perl?

 

 
Powered by phpBB® Forum Software