 | 
August 11th, 2008, 08:16 AM
| | Newbie | | Join Date: Aug 2008 Location: Melbourne, Australia Age: 47
Posts: 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.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
| 
August 11th, 2008, 12:00 PM
|  | Forum Leader | | Join Date: May 2007 Location: New Hampshire
Posts: 2,156
| |
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
| 
August 12th, 2008, 01:45 AM
| | Newbie | | Join Date: Aug 2008 Location: Melbourne, Australia Age: 47
Posts: 3
| | 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. - 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; August 12th, 2008 at 02:57 AM.
Reason: Please use the [code][/code] tags
| 
August 12th, 2008, 08:08 AM
|  | Expert | | Join Date: Dec 2007 Age: 24
Posts: 366
| |
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. -
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
-
-
| 
August 13th, 2008, 08:30 AM
| | Newbie | | Join Date: Aug 2008 Location: Melbourne, Australia Age: 47
Posts: 3
| |
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. -
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
-
-
| |  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|