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

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

Searching Through the Text of Each File on a WebSite, Part 2

In Part 1 we discussed how to build the interface for our search engine. Now comes the fun part, writing the ASP code for Search.asp! I had a lot of fun writing this code. I hope you decide to use it, but if you do, there are a few warnings. First, the code is intensive, and I cannot assure its efficiency. A more efficient approach to text-based searching is to use Microsoft's Index Server, or some other Indexer. That being said, let's go on to the code! We'll take some time at the end of the article to analyze this searching algorithm and to see if we can't find any bottlenecks or places where we can improve performance.

- continued -

First, we will read in the form field values entered by the user. This is done, of course, using the Request object:

'Read in the keywords entered by the user Dim strKeywords strKeywords = Request("terms") 'Split up each of the terms on spaces, placing 'the results into an array Dim termsArray termsArray = split(strKeywords," ") 'Set the boolean search option Dim bolAnd If Request("boolean") = "AND" then bolAnd = True else bolAnd = False Dim section section = Request("selSearchWhere")

Note that we read in the terms entered by the user, storing them in strKeyWords. Next, we use the split function to parse the terms into an array, tempArray. For more information on split, be sure to read Parsing with join and split, a great article. Since our searching algorithm will use the FileSystemObject to iterate through a particular folder, we need to obtain the physical path for the folder. What folder we are interested in depends upon what section the user wants to search. Therefore, the following code will obtain the correct physical path:

'Get the dirs to search If section = "HUMOR" then section = Server.MapPath("../humor/") else section = Server.MapPath("../webtech/") end if

Note that we use Server.MapPath to return the correct physical directory (like C:\Inetpub\wwwroot\humor\. If you're unfamiliar with Server.MapPath you can read the technical documentation.

Now, how in the world are we going to page our results? I chose to use the following approach, assuming that we could "remember" the last file found in the search:

  • Skip ahead to the last file we found
  • List next n matches
  • If we listed n matches, then we can assume that there are more matches, so save the last file listed by passing it through the querystring when the user clicks on the Show more results link.

So, we need to pass a parameter through the querystring when the user clicks Show more results. This parameter will contain the last file found in our search. Note that when the user first executes this search, we are not passing any value in, since we have not yet performed ther search, and therefore do not have to report a last file found parameter. The following code reads in the value of the last file listed.

'What page are we on? Dim strLastFile strLastFile=Request("lf")

Next, we need to create an instance of the FileSystemObject to obtain the information on the directory that we are interested in searching. We will obtain this information using the GetFolder method of the FileSystemObject. We will then pass this Folder object to a function that will search through the files of the folder, returning those that contain the text requested by the user's search. The following code will create a FileSystemObject instance, and use the GetMethod to obtain the information for the folder specified by section, the variable that contains the physical path to the folder the user is interested in searching (see the code two examples above). If you are new to the FileSystemObject, I strongly recommend you read the exhaustive FileSystemObject FAQ.

Dim objFSO, objFolder Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 'Grab the information of the folder specified by section Set objFolder = objFSO.GetFolder(section) Set objFSO = Nothing

Note that section contains the physical path to the folder that the user indicated she wanted to search. In Part 3 we will look at how to create a recursive function that will iterate through the folder and subfolders of section, listing those files that contain the appropriate text.

  • Read Part 3
  • Read Part 1

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