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
4 1750
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
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;
-
}
-
}
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
-
-
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. -
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
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
| |