Help with file search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sutharsan Nagasun
    New Member
    • Aug 2008
    • 3

    Help with file search

    Hi, I am new to Perl. I need help with file search for the following scenario.
    Currently as part of the archiving process, we have archived the files under
    /$rootdir/Archive/yyyy directory where yyyy is year.
    During the archiving process, for each day, yyyymmdd_trn.ls t file and yyyymmdd_trn.ta r.gz files are created where lst file will contain the names of all files that have been archived under yyyymmdd_trn.ta r.gz file.

    I am in the process of extracting (gunzip) given file from appropriate .tar.gz file into /$rootdir/Archive/Extract directory for reuse.

    I am able to extract the required file, when I know the archive date.
    The problem is when I don't know the archive date, I will be able to search the file name that I want to extract and identify the .lst file name.
    Eg: The file that I need to extract is abc999877777.xm l
    This name should be in contents of one of the .lst file under /$rootdir/Archive/yyyy
    I want to search contents of all .lst file under Archive directry for match abc999877777.xm l to identify which year (yyyy) directory and name of the .lst file.

    Can you help me to achive this? Quick response is appreciated? Thanks.
    Regards,
    Sutharsan
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Can we see what code you have tried thus far to do this? We can help you to get it working once we can see your attempt(s).

    Regards,

    Jeff

    Comment

    • Sutharsan Nagasun
      New Member
      • Aug 2008
      • 3

      #3
      Originally posted by numberwhun
      Can we see what code you have tried thus far to do this? We can help you to get it working once we can see your attempt(s).

      Regards,

      Jeff
      Thanks Jeff for the quick response.
      Here is a section of the code that involved with the extract.

      Code:
      sub Process($)
      {
          my($list) = @_;
          my($RC,$ArchiveDIR,$DestDIR,$ArchType,$ListFile,$ArchFileFnd);
      
          $RC=0;
      
          # Setup.  Work out the archive and destination directories
          #         and the changeable part of the archive name.
          if ( $ExtractPath eq "$TRANSDIR" && $ExtractFile ) {
              $ArchiveDIR="$TRANSDIR/Archive";
              $DestDIR="$TRANSDIR/Archive/Extract";
              $ArchType="Trn";
          } elsif (  $ExtractPath eq "$MSGDIR" && $ExtractFile ) {
              $ArchiveDIR="$MSGDIR/Archive";
              $DestDIR="$MSGDIR/Archive/Extract";
              $ArchType="Msg";
          } elsif ( $ExtractPath ne "$TRANSDIR" && $ExtractPath ne "$MSGDIR" ) {
              print "Error:Currently only Transactions and Messages Archiving allowed. \n";
              print "For other Archiving contact system admin. \n";
              $RC=4;
          } else {
             print "Error: Unknown error encountered during Archiving extract. \n";
             $RC=8;
          }
      
          $ListFile="$ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.lst";
          print "$DestDIR \n";
          print "$ListFile \n";
      
          if ( -e $ListFile ) {
              print "$ListFile exists \n";
              $ArchFileFnd="Y";
          } else {
             chdir ($ArchiveDir);
          # This is the slot where I need the help.
          # I have to search for the location and the name of a .lst of that contains $ExtractFile name. 
          # This will lead me to the tar.gz file for extract.
          #  
      
          #  print "$ListFile does not exist \n";
          }
      
         if ( $ArchFileFnd ) {
             chdir($DestDIR);
            if ( -f "$ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz" ) {
               system("gunzip -c $ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz | tar xvf - $ExtractFile");
            } else {
                   print "Error: Archive missing: $ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz\n";
                   $RC=8;
                   }
      
            if  ( $RC == 0 && ! -f "${ExtractFile}" ) {
                # Still no filename after extract. Return a warning
                print "${ExtractFile} not found after the extract";
                $RC=4;
                }
         }
      Last edited by eWish; Aug 12 '08, 01:57 AM. Reason: Please use the [code][/code] tags

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        I am not sure if I understood your requirement completely. You can make use of File::Find module to recursively search inside a directory.
        If you want to search all .lst files inside all year directories, for a particular file in the list ($ExtractFile) and get the list of target zip files, you can make use of this piece of code.
        Code:
        use File::Find;
        #
        #
        #
        # 
        my @targets;
        
        find( 
          sub { 
             my $file =$File::Find::name;#contains entire path of file
             if((-f $file) && $file=~/\.lst/) {
             open(F,"$file") or die "error:$!";
            my @file= <F>;
            if(grep /$ExtractFile/,@file){
              my $reqyear = $1 if($file=~/\/(\d{4})\/.+?$/);##get the year
              $file=~s/\.lst/\.tar\.gz/;  # get the target zip file
              push @targets,$file; # get the list of targets
                   }
               }  }, 
            $ArchiveDIR); ## recursively search in $ArchiveDIR

        Comment

        • Sutharsan Nagasun
          New Member
          • Aug 2008
          • 3

          #5
          Thanks Nithin,

          Implemented your suggested code and it works perfectly.

          Thank you again for your help.

          Regards,
          Sun


          Originally posted by nithinpes
          I am not sure if I understood your requirement completely. You can make use of File::Find module to recursively search inside a directory.
          If you want to search all .lst files inside all year directories, for a particular file in the list ($ExtractFile) and get the list of target zip files, you can make use of this piece of code.
          Code:
          use File::Find;
          #
          #
          #
          # 
          my @targets;
          
          find( 
            sub { 
               my $file =$File::Find::name;#contains entire path of file
               if((-f $file) && $file=~/\.lst/) {
               open(F,"$file") or die "error:$!";
              my @file= <F>;
              if(grep /$ExtractFile/,@file){
                my $reqyear = $1 if($file=~/\/(\d{4})\/.+?$/);##get the year
                $file=~s/\.lst/\.tar\.gz/;  # get the target zip file
                push @targets,$file; # get the list of targets
                     }
                 }  }, 
              $ArchiveDIR); ## recursively search in $ArchiveDIR

          Comment

          Working...