473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
Aug 11 '08 #1
4 1754
numberwhun
3,509 Expert Mod 2GB
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
Aug 11 '08 #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
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.    }
Aug 12 '08 #3
nithinpes
410 Expert 256MB
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.  
Aug 12 '08 #4
Thanks Nithin,

Implemented your suggested code and it works perfectly.

Thank you again for your help.

Regards,
Sun


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.  
Aug 13 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
3
by: cuties | last post by:
Hi all.... i'm very new to this programming language. i'm required to fulfill this task in the company i'm doing my practical. i hope i can get guide for my problem... Here is the script i...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
4
memoman
by: memoman | last post by:
Can any body help me in that program ??? mail me if anybody could reach any -helpfull- thing Write a C++ program that namely insert, delete, and search in a fixed record length file (containing...
1
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
2
by: Slippy27 | last post by:
I'm trying to modify a find/replace script which iterates through a file A and makes replacements defined in a csv file B. My original goal was to change any line in file A containing a search string...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.