[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

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
0
paulwhelan
Asked:
paulwhelan
1 Solution
 
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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
 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now