Hi all,
I have been given a task to search for strings in a file that is encoded. I need to display the file name only when all the 3 strings which i provide are present in the file name.
i first try to get a list of files according from the directory according to the value passed in argument 1. For each file in the list i then try to search for a strings in the file
- The strange thing is that it never goes into the loop
if i replace loop for search_pattern3 and put it before search_pattern2 then the code does not go into search_pattern3. So it is always the second loop. i have tried to enter same values for string1,string2,string3 but still it does not matches.
Could someone help ?
I have been given a task to search for strings in a file that is encoded. I need to display the file name only when all the 3 strings which i provide are present in the file name.
i first try to get a list of files according from the directory according to the value passed in argument 1. For each file in the list i then try to search for a strings in the file
Code:
use Time::localtime;
use POSIX qw/strftime/;
use File::Find;
use File::stat;
use IO::Handle;
use File::copy;
use Unicode::String ;
use Cwd;
use Encode;
use PerlIO::encoding;
Unicode::String->stringify_as( 'utf8' );
sub trim($);
$dir = "C:\\Temp\\t\\";
($File_pattern,$search_pattern,$search_pattern2,$search_pattern3) = @ARGV;
$SRC_FLG1=0;
$SRC_FLG2=0;
$SRC_FLG3=0;
#print $search_pattern2;
print "Searching $search_pattern,$search_pattern2,$search_pattern3 in $dir......\n";
$search_pattern = trim($search_pattern);
$search_pattern2= trim($search_pattern2);
$search_pattern3= trim($search_pattern3);
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
opendir ( DIR, $dir ) || die "Error in opening dir $dirname\n";
find (\&Search, "$dir");
sub Search {
if ((/\.msg$/) && (/$File_pattern/) )
{
print $_;
foreach $x ($_) {
my $filename = $x;
print "\n--------------------------------------------------\n";
open(my $in, '<:raw', $filename) || die "Couldn't open file: $!";
my $text = do { local $/; <$in> };
close $in;
my $content = decode('UTF-16LE', $text);
foreach $line ($content)
{ print "search $search_pattern in $content";
if ($line =~ (m/$search_pattern/gi))
{
print "\nYes1";
$SRC_FLG1=1;
}
print "search $search_pattern2 in $content";
if ($line =~ (m/$search_pattern2/gi))
{
print "\nYes2";
}
print "search $search_pattern3 in $content";
if ($line =~ (m/$search_pattern3/gi))
{
print "\nYes3";
$SRC_FLG3=1;
}
if ($SRC_FLG1==1 && $SRC_FLG2==1 && $SRC_FLG3==1)
{
print "File found : $filename\n";
}
print $SRC_FLG1,$SRC_FLG2,$SRC_FLG3;
$SRC_FLG1=0;
$SRC_FLG2=0;
$SRC_FLG3=0;
}
}
}
print "\n--------------------------------------------------\n";
closedir(DIR);
chdir("..");
- The strange thing is that it never goes into the loop
Code:
if ($line =~ (m/$search_pattern2/gi))
if i replace loop for search_pattern3 and put it before search_pattern2 then the code does not go into search_pattern3. So it is always the second loop. i have tried to enter same values for string1,string2,string3 but still it does not matches.
Could someone help ?