Add a search utility to your site

This search utility will allow you to search for terms or phrases within your site, and the resulting page will show your visitors the title of the page and a link to it.

You only need to include the form bellow in your page:
 

searchfiles.html
<form method="POST" action="/cgi-bin/searchfiles.asp">
<table border=0 cellpadding=5 bgcolor=AAAAAA><tr>
<td align=center>
Search term or phrase &nbsp; <input type=text name=term size=15> &nbsp; <input type=submit value="search"></td>
</tr></table>
</form>

And you will need to create a file with the content in the table bellow named searchfiles.asp in the corresponding directory. Active Server Pages must be available in the server. Instructions are in the bottom.

 

searchfiles.asp
<%
SearchTerm = request.form("term")

Novalid=" a b c d e f g h i j k l m n o p q r s t u v w x y z yes no and more"
NovalidResponse ="Go back and try again. Your search term is very common"
 

if instr(SearchTerm,Novalid)=0 then

Response. Write ("<CENTER><H2>Search results</H2></CENTER>")
 

   FolderName= "FolderName"
   ShowList()

else
Response.Write NovalidResponse
End if

SearchResponse=""
%>

<%
Sub ShowList()
FolderToCheck = server.mappath("\") &"/" & FolderName & "/"
 Dim fs, f, f1, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(FolderToCheck)
 Set fc = f.Files
 For Each f1 in fc

         Wfile = f1.name
          if right(Wfile, 5)=".html" OR right(Wfile, 4)=".htm" then
            Wfile2 = FolderToCheck & Wfile
            Set fs = CreateObject("Scripting.FileSystemObject")
            Set a = fs.OpenTextFile(Wfile2)
            ct = a.ReadAll
            a.close
            ct2=lcase(ct)
            SearchTerm2 = lcase(SearchTerm)

                  if instr(ct2,SearchTerm2)>0 then

                      if instr(ct,"</title>")>0 then
                  longitud=instr(ct,"</title>")
                  longitud=longitud -1
                          ct=left(ct,longitud)
                          longitud2=len(ct)
                  longitud=instr(ct,"<title>")
                  longitud=longitud2 -longitud -6
                          ct=right(ct,longitud)
                     else
                        if instr(ct,"</TITLE>")>0 then
                    longitud=instr(ct,"</TITLE>")
                    longitud=longitud -1
                            ct=left(ct,longitud)
                            longitud2=len(ct)
                    longitud=instr(ct,"<TITLE>")
                    longitud=longitud2 -longitud -6
                            ct=right(ct,longitud)
             end if
                     end if

                    SearchResponse= SearchResponse "<TR><TD>" ct "</TD><TD ALIGN=CENTER VALIGN=MIDDLE>" " <A HREF=" "/" FolderName "/" Wfile "> More </A></TD></TR>"
          longitud=0
                  ct=""

                  end if
          end if

Next

Response.Write ("<HTML><HEAD><TITLE>Search results</TITLE></HEAD><BODY BGCOLOR=FFFFFF><CENTER>") 

Response.Write ("<TABLE BORDER=0 WIDTH=550><TR><TD BGCOLOR=C0C0C0><FONT SIZE=5><B>Search Results</B></FONT></TD></TR></TABLE>") 

Response.Write ("<TABLE BORDER=0 WIDTH=550>") 
if SearchResponse<>"" then 
Response.Write SearchResponse
else
Response.Write ("<TR><TD>The specified term or phrase was not found</TD></TR>")
end if
Response.Write ("</TABLE>")

Response.Write ("</CENTER></BODY></HTML>")

End Sub
%>
 


You may include the terms you do not want to be searched in the variable "Novalid". In case a term included in "Novalid" is request, write your response in the variable "NovalidResponse". 

"Search results" is the text which will be shown in the top of the search results page.

"FolderName" is the directory you want to search. Only ".htm" and ".html" files in this directory will be searched. Subdirectories are not search. In case you want to search your top directory, set FolderName="". This script is only useful in top directories only. I do not recommend to check more than 50 files (Try to be friendly with your server!) 

This script will show as a response a list of the titles of  pages with the requested term of phrase.


 

AspTutorial.info