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.
First, we will read in the form field values entered by the user. This is done, of course, using the
Note that we read in the terms entered by the user, storing them in
we use the
split function to parse the terms into an array,
more information on
split, be sure to read Parsing with
join and split, a great article. Since our searching algorithm will use the
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:
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 resultslink.
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.
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
and use the
GetMethod to obtain the information for the folder specified by
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.
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.