Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 11th, 2008, 08:16 AM
Newbie
 
Join Date: Aug 2008
Location: Melbourne, Australia
Age: 47
Posts: 3
Default 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.lst file and yyyymmdd_trn.tar.gz files are created where lst file will contain the names of all files that have been archived under yyyymmdd_trn.tar.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.xml
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.xml 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
Reply
  #2  
Old August 11th, 2008, 12:00 PM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

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
Reply
  #3  
Old August 12th, 2008, 01:45 AM
Newbie
 
Join Date: Aug 2008
Location: Melbourne, Australia
Age: 47
Posts: 3
Default

Quote:
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.

Expand|Select|Wrap|Line Numbers
  1. sub Process($)
  2. {
  3.     my($list) = @_;
  4.     my($RC,$ArchiveDIR,$DestDIR,$ArchType,$ListFile,$ArchFileFnd);
  5.  
  6.     $RC=0;
  7.  
  8.     # Setup.  Work out the archive and destination directories
  9.     #         and the changeable part of the archive name.
  10.     if ( $ExtractPath eq "$TRANSDIR" && $ExtractFile ) {
  11.         $ArchiveDIR="$TRANSDIR/Archive";
  12.         $DestDIR="$TRANSDIR/Archive/Extract";
  13.         $ArchType="Trn";
  14.     } elsif (  $ExtractPath eq "$MSGDIR" && $ExtractFile ) {
  15.         $ArchiveDIR="$MSGDIR/Archive";
  16.         $DestDIR="$MSGDIR/Archive/Extract";
  17.         $ArchType="Msg";
  18.     } elsif ( $ExtractPath ne "$TRANSDIR" && $ExtractPath ne "$MSGDIR" ) {
  19.         print "Error:Currently only Transactions and Messages Archiving allowed. \n";
  20.         print "For other Archiving contact system admin. \n";
  21.         $RC=4;
  22.     } else {
  23.        print "Error: Unknown error encountered during Archiving extract. \n";
  24.        $RC=8;
  25.     }
  26.  
  27.     $ListFile="$ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.lst";
  28.     print "$DestDIR \n";
  29.     print "$ListFile \n";
  30.  
  31.     if ( -e $ListFile ) {
  32.         print "$ListFile exists \n";
  33.         $ArchFileFnd="Y";
  34.     } else {
  35.        chdir ($ArchiveDir);
  36.     # This is the slot where I need the help.
  37.     # I have to search for the location and the name of a .lst of that contains $ExtractFile name. 
  38.     # This will lead me to the tar.gz file for extract.
  39.     #  
  40.  
  41.     #  print "$ListFile does not exist \n";
  42.     }
  43.  
  44.    if ( $ArchFileFnd ) {
  45.        chdir($DestDIR);
  46.       if ( -f "$ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz" ) {
  47.          system("gunzip -c $ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz | tar xvf - $ExtractFile");
  48.       } else {
  49.              print "Error: Archive missing: $ArchiveDIR/$ExtractYr/${ExtractDate}_${ArchType}.tar.gz\n";
  50.              $RC=8;
  51.              }
  52.  
  53.       if  ( $RC == 0 && ! -f "${ExtractFile}" ) {
  54.           # Still no filename after extract. Return a warning
  55.           print "${ExtractFile} not found after the extract";
  56.           $RC=4;
  57.           }
  58.    }

Last edited by eWish; August 12th, 2008 at 02:57 AM. Reason: Please use the [code][/code] tags
Reply
  #4  
Old August 12th, 2008, 08:08 AM
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Age: 24
Posts: 366
Default

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.
Expand|Select|Wrap|Line Numbers
  1. use File::Find;
  2. #
  3. #
  4. #
  5. my @targets;
  6.  
  7. find( 
  8.   sub { 
  9.      my $file =$File::Find::name;#contains entire path of file
  10.      if((-f $file) && $file=~/\.lst/) {
  11.      open(F,"$file") or die "error:$!";
  12.     my @file= <F>;
  13.     if(grep /$ExtractFile/,@file){
  14.       my $reqyear = $1 if($file=~/\/(\d{4})\/.+?$/);##get the year
  15.       $file=~s/\.lst/\.tar\.gz/;  # get the target zip file
  16.       push @targets,$file; # get the list of targets
  17.            }
  18.        }  }, 
  19.     $ArchiveDIR); ## recursively search in $ArchiveDIR
  20.  
  21.  
Reply
  #5  
Old August 13th, 2008, 08:30 AM
Newbie
 
Join Date: Aug 2008
Location: Melbourne, Australia
Age: 47
Posts: 3
Default

Thanks Nithin,

Implemented your suggested code and it works perfectly.

Thank you again for your help.

Regards,
Sun


Quote:
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.
Expand|Select|Wrap|Line Numbers
  1. use File::Find;
  2. #
  3. #
  4. #
  5. my @targets;
  6.  
  7. find( 
  8.   sub { 
  9.      my $file =$File::Find::name;#contains entire path of file
  10.      if((-f $file) && $file=~/\.lst/) {
  11.      open(F,"$file") or die "error:$!";
  12.     my @file= <F>;
  13.     if(grep /$ExtractFile/,@file){
  14.       my $reqyear = $1 if($file=~/\/(\d{4})\/.+?$/);##get the year
  15.       $file=~s/\.lst/\.tar\.gz/;  # get the target zip file
  16.       push @targets,$file; # get the list of targets
  17.            }
  18.        }  }, 
  19.     $ArchiveDIR); ## recursively search in $ArchiveDIR
  20.  
  21.  
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles