
Jean
User

Apr 21, 2002, 11:18 PM
Post #3 of 10
(1078 views)
|
|
Re: [BrightNail] A challenge: Email Extractor from all directories to text file???
[In reply to]
|
Can't Post
|
|
Doesn't seem like a challenge to me either ... You'll have to improve the email regexp that is passed to the function (grabs too much junk), but in general it works... [perl] #!/usr/bin/perl -w # ------------------------------------------------------------------------ use strict; my $StartDir = shift(@ARGV) || '.'; my %data; sub ScanDir($$;$); ############################################################################ sub ScanFile($$) ############################################################################ { my $file = shift(@_); my $expr = shift(@_); print "FILE: $file\n"; open(FILE, $file) or die "Error opening file $file - $!\n"; while (my $line = <FILE>) { while ($line =~ /($expr)/g) { $data{$1} = 1; } } close(FILE); } ############################################################################ sub ScanDir($$;$) ############################################################################ { my $dir = shift(@_); my $filetype = shift(@_); my $expr = shift(@_) || '.*'; my $file; my @dirs; my $success; # Scan the passed dir. $success = opendir(DIR, "$dir"); if (!$success) { warn "Unable to open directory $dir\n"; return; } while ($file = readdir(DIR)) { if ($file !~ /^\.{1,2}$/) { # Do nothing in case of '.' or '..' # Add path to the found file name. ($dir =~ /\/$/)?($file = $dir.$file):($file = "$dir/$file"); if (-d $file) { # Take care of subdirs. push(@dirs, $file); } elsif ( (-f $file) && ($file =~ /$filetype/)) { ScanFile($file, $expr); } } } closedir (DIR); while ($file = shift(@dirs)) { # Call the function recursively in order to scan found subdirs. ScanDir($file, $filetype, $expr); } } ############################################################################ # main() ############################################################################ { # [root dir] [file type regex] [email regex] ScanDir($StartDir, '.+\.(html|htm)', '[^\s]+@[^\s.]+\.[^\s]+'); for my $key (keys %data) { print "$key\n"; } print "Done.\n"; } [/perl]
Jean Spector SQA Engineer @ Exanet jean.spector@softhome.net There are only 10 types of people in the world - Those who understand binary, and those who don't.
|