• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

Finding Filenames with the query as part of the filename

Hi guys!
I have the following which u guys kindly helped me with.
At the moment, it does the following...

1) Go to http://localhost/search.html
Enter in a string that you want to find, click on Submit
2) This takes you to:
http://localhost/cgi-bin/search.cgi
where the output is the names of files that contain the string you entered in the form, WITHIN the actual file, not as part of their filename.
What Id like to do is modify the following so that, instead of the results returning files that contain the query string WITHIN the actual file returned, instead - have the results return files that have the query string as part of their filename.
eg. if you enter in 'test' as the string to find, then successful results might be test.doc, testing.txt, thisisatest.txt
At the moment, the following code only returns results that contain the query string INSIDE the file, so successful results for a query string of 'test'  might be document.doc, exceldoc.xls, where both of these have the text 'test' in the document.

Any help greatly appreciated.

================================================== search.html
<form action="/cgi-bin/search.cgi" method="post">
<input type="text" name="query" size="50" />
<input type="submit" />
</form>
=================================================== search.cgi
#!c:/perl/bin/perl.exe          
use strict;                                                      
use CGI qw(:standard);            
use File::Find;
     
my $query = param("query");

print header();
print start_html();
print "\n<p>For the query $query, these results were found:</p>\n<ol>\n";

undef $/;

find      ( sub      {
       return if ($_ =~ /^\./);                        
       return unless ($_ =~ /\.txt/i || /\.xls/i);    
       stat $File::Find::name;
       return if -d;
       return unless -r;
                                     
       open(FILE, "< $File::Find::name") or return;
      my $string = <FILE>;
      close (FILE);
                                   
      return unless ($string =~ /\Q$query\E/i);
                                   
      my $page_title = $_;            
       if ($string =~ /<title>(.*?)<\/title>/is)      {
           $page_title = $1;
       }
       
       my $target=$File::Find::name;

($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
#print "<li><a href=\"$target\">$page_title</a></li>\n";
print "<li><a href=\"$target\">$page_title</a></li> $File::Find::dir\n";
      },
        '/wamp/www/'
        );
                   
        print "</ol>\n";
      print end_html();
============================================================ End of file.


0
Simon336697
Asked:
Simon336697
1 Solution
 
mjcoyneCommented:
Assuming all else about search.cgi is working, and you no longer care if the query is in the file itself, only whether it's in the title:

#!c:/perl/bin/perl.exe
use strict;                                                      
use CGI qw(:standard);            
use File::Find;
     
my $query = param("query");

print header();
print start_html();
print "\n<p>For the query $query, these results were found:</p>\n<ol>\n";

undef $/;

find      ( sub      {
    return if ($_ =~ /^\./);                        
    return unless ($_ =~ /\.txt/i || /\.xls/i);    
    stat $File::Find::name;
    return if -d;
    return unless -r;
    return unless ($File::Find::name =~ /\Q$query\E/i);  # added line
    open(FILE, "< $File::Find::name") or return;
    my $string = <FILE>;
    close (FILE);                    
    my $page_title = $_;            
    if ($string =~ /<title>(.*?)<\/title>/is) {
        $page_title = $1;
    }
    my $target=$File::Find::name;
    ($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
    print "<li><a href=\"$target\">$page_title</a></li> $File::Find::dir\n";
},
'/wamp/www/'
);
                   
print "</ol>\n";
print end_html();
0
 
Simon336697Author Commented:
Thanks mj...works really great.
Thanku
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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