Hi The script seems to be working ok with a list files to read a dir from but i got a dir with thousands of files and it does not search through all files, when i do a print of the read dir it comes up with less than ahindred. Could some one please help me, not sure which part is wrong in the code. Need it read and search the whole dir
Code:
#!/usr/bin/perl
#use strict;
use warnings;
use File::Copy;
$| = 1;
# Contains a list of product numbers
my $prodfile = "/home/prod.txt";
#Contains a bunch of flat files where i would take a product number and check line in each file of that dir and if they have a line containing the prod number it will print to the output file.
my $srcdir = "/home/archive/";
my $finalfile = "/home/output.txt";
# Check for command line arguments
if ($#ARGV != 0) {
die "Usage: $0 You need to enter <search string> \n";
} else {
#print "Arg : $#ARGV\t @ARGV \n";
$search = "ARGV[0]";
}
print "##### Reading Line from File ##### \n\n\n\n";
open(PRODFILE, "< $prodfile") or die (" Could not open Product File $!");
while (my $product = <PRODFILE>) {
chomp($product);
##### Opens Dir and Lists files in archive dir #####
opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
while (my $files = readdir(DIR)) {
print " This is file $files \n";
if ((-f == "$files") && (("$files")=~ /^$search/)) {
print " This product is : $product \n";
### Open file and searching for matching products ###
open(INFILE2,"< $srcdir/$files") || die(" Could not open INFILE2 File!");
open(OUTFILE2, ">> $finalfile") || die("Could not OUTFILE2 open file $!");
# while (my $findprod = <INFILE2>) {
# chomp($findprod);
# if($product =~ /$findprod/) {
# print OUTFILE2 "$findprod \n";
# print "******* Match Found: $findprod ********* \n\n"
# }
#### Checking in line in the file and comaring with prod.txt to see if there is a match ######
while (my $findprod = <INFILE2>) {
chomp($findprod);
# print "qq~if ($product =~ /$findprod/)\n~";
if ($findprod =~ /$product\|/) {
print "******* Match Found: $findprod ********* \n\n";
sleep 2;
print OUTFILE2 "$findprod \n";
}
}
}
}
}
close(DIR);
close OUTFILE2;
close INFILE2;
close PRODFILE;
Comment