search all files in dir

id like a perl script that searches all the files in a dir

if what the users enters in the search box is in any file
or all files it will return a link to that file which can be clicked
on so the file can be viewed

thanks
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

olthoffCommented:
opendir(DIR, "dirtoopen");
foreach ($File = readdir(DIR)) {
  open(FILE, $File) || die("Can't open file\n");
  while (<FILE>) {
    if (m/$UserEntry/) { push(@Files, $File);
  };
  close(FILE);
};
close(DIR);

#### Now generate the HTML
0
paulwhelanAuthor Commented:
i cant seem to get this to work
can u post a working version
thanks
paul
0
ozoCommented:
while( defined($File = readdir(DIR)) ){
#or
foreach $File (readdir(DIR) ){
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

paulwhelanAuthor Commented:
where does that line go in?
can someone repost the full code?
thanks
paul
0
jsatterlCommented:
my $File;
opendir(DIR, "dirtoopen");
foreach $File (readdir(DIR)) {
  open(FILE, $File) || die("Can't open file\n");
  while (<FILE>) {
    if (m/$UserEntry/) {
       push(@Files, $File);
    };
  };
  close(FILE);
 };
close(DIR);
0
paulwhelanAuthor Commented:
i cant seem to follow this code
can someone repost the html and perl to search all files in a dir?
thanks
paul
0
maxkirCommented:
HTML:
<FORM><INPUT name=to_search><input type=submit></FORM>

Perl:
#!/usr/local/bin/perl
use CGI qw/:cgi/

$dir_with_files="/some/dir";

print "Content-type: text/plain\n\n";

opendir(DIR, $dir_with_files);

if (!param)
    exit; # no search string

$UserEntry = param('to_search');


foreach $File (readdir(DIR))
{
  open(FILE, $File) || die("Can't open file\n");
  while (<FILE>)
  {
    if (m/$UserEntry/)
    {
        print $File ."\n";
    }
  }
  close(FILE);
}
close(DIR);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
paulwhelanAuthor Commented:
could u put in some options like
case sensitivity and max number of results and anything else that might be of interest
thanks
0
maxkirCommented:
Case insensitive serach:
m/$UserEntry/i

Case sensitive serach:
m/$UserEntry/

Max number of results is achived via simple counter in
foreach $File cycle with 'last;' command when counter exceeds maximum amount of files.

Sorry, but I'm not going to write the whole program for you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.