Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening and searching files within a Dir 1

Status
Not open for further replies.

MAWarGod

Programmer
Feb 15, 2002
352
US
# Get file Count #
$total = 0;
$dirname = "/MY/DIR";
opendir(DIR, $dirname);
while (<DIR>) {

Now where do I go from here to search each doc or txt file for names or quotes within them?

MA WarGod

I believe if someone can think it, it can be programmed
 
see what I am trying to do is open a dir that contains book files and I want to search the the files to match the input name,word,quote,# ect

MA WarGod

I believe if someone can think it, it can be programmed
 
one possible way:

Code:
#!perl
use strict;   

my @quotes  = ('I have never let my schooling interfere with my education',
            'The important thing is not to stop questioning',
);
my %results = ();
my $dirname = '/MY/DIR';
chdir('/MY/DIR') or die "$!";
opendir(DIR, '.') or die "Can't open $dirname: $!";
my @dirs = readdir(DIR);
close(DIR);
for(@dirs){
   next if ($_ eq '.' or $_ eq '..');
   open(FH,$_) or next;
   my $string = do { local $/; <FH> };
   close(FH);
   foreach my $quotes (@quotes){
      push @{$results{$_}},$quotes if ($string =~ /$quotes/i);
   }
}
foreach my $files (sort keys %results) {
   print "File '$files' has the following quotes:\n";
      print "\t$_\n" for @{$results{$files}};
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top