When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Sunday, October 24, 1999

A Text-Based Search Engine


Have you wanted to allow your users to search through your website, checking for files that contain a particular string? Using the FileSystemObject, you can search through all of the files in a particular directory that contain a particular string. To allow for this functionality, you will need two ASP pages: one that presents the user a form to enter the string they wish to search for; the second needs to take this string, iterate through all of the files in a particular folder, and determine which of these files contain the string. We'll start by coding the first ASP page, Search.asp, which contains a simple form:

- continued -

<HTML> <BODY> <FORM METHOD=POST ACTION="TextSearch.asp"> Enter text to search for: <INPUT TYPE=TEXT NAME=SearchText> <P> <INPUT TYPE=SUBMIT VALUE="Begin Search!"> </FORM> </BODY> </HTML>

A screen shot of the search form. A screen shot of this form can be seen on the right. Into the search text box, the user will enter his or her search. The results of this will be send on over to TextSearch.asp when the form is submitted.

TextSearch.asp needs to perform the following tasks:

  • Iterate through each file of a particular directory (we'll use the root web directory for this example, although you can change it quite easily)
  • Open each file and read its contents into a string variable
  • Search that string variable for any occurrences of the text string entered by the user in the form in Search.asp
  • If there is a match, display a link to that particular file

To accomplish these tasks we are going to use the FileSystemObject. If you are unfamiliar with FSO, please read Reading/Writing Text Files. Let's look at the code for TextSearch.asp!

<HTML><BODY>
<B>Search Results for <%=Request("SearchText")%></B><BR>

<%
Const fsoForReading = 1

Dim strSearchText
strSearchText = Request("SearchText")

'Now, we want to search all of the files
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")


Dim objFolder
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))

Dim objFile, objTextStream, strFileContents, bolFileFound
bolFileFound = False

For Each objFile in objFolder.Files
  If Response.IsClientConnected then
    Set objTextStream = objFSO.OpenTextFile(objFile.Path,fsoForReading)

    strFileContents = objTextStream.ReadAll

    If InStr(1,strFileContents,strSearchText,1) then
       Response.Write "<LI><A HREF=""/" & objFile.Name & _
                      """>" & objFile.Name & "</A><BR>"

       bolFileFound = True
    End If

    objTextStream.Close
  End If
Next

if Not bolFileFound then Response.Write "No matches found..."

Set objTextStream = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
%>
</BODY></HTML>

That's it! This code iterates through all the files, searching for a particular string. If no matches are found, No matches found... is displayed. Pretty neat, eh?


Attachments:

  • Download the source for Search.asp in text format
  • Download the source for TextSearch.asp in text format

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article