PDA

View Full Version : Search Page - Functional, but need to display page description.


biggs27
10-21-2002, 06:59 PM
I have a search page (code below) that is providing the results I'm looking for, but I'm having a problem making it display additional information. Specifically, I want to display the description (<meta name="description" content="Blah de blah.">) of the page right under the page title and above the path.

This input to the included page is a single field form named "txtKeyWord."

Here is the code. I've cut out some of the unnecessary html in an effort to leave you with the meat. The output area is in red.

Please let me know if you have any other questions, and thanks for taking a look!

Brian

<%@ Language=VBScript %>

<%
Response.Buffer = True

' Set up some FileSystem objects.
dim objFS, objFolder, objFiles, File, FileName, objCheckFile, strSitePath
set objFS = server.CreateObject("Scripting.FileSystemObject")

dim strListofFiles, arrFileList, strPageTitle, iReturnCount
dim objFolders

strListofFiles = ""
iReturnCount = 0

' The search values, search space, and the return value
dim strKeyWord, strFileContents, returnValue, arrKeyWords
dim bKeyWordFound, i, j

' Get the Physical path to the current directory
strSitePath = Server.MapPath(".")

' Grab the keyworlds and split them into an array
strKeyWord = Trim(request("txtKeyWord"))
arrKeyWords = split(strKeyWord," ")

' Get the list of files.
Set objFolder = objFS.GetFolder(strSitePath)


' Sub that grabs all the ".htm" & ".html" files in the given folder and
' recurses through all subfolders.
sub getCompleteFileList(inFolder, Path)

dim oFiles, oFolders, Folder, sPath

' Get all the files and subfolders.
Set oFiles = inFolder.Files
Set oFolders = inFolder.SubFolders

' If we have a subfolder we need the partial path
if (Path <> "") then
sPath = Path + "/"
end if

' Add each ".htm" & ".html" file to the list
For Each File in oFiles
FileName = File.Name

if((Lcase(right(FileName,4)) = ".htm") OR (Lcase(right(FileName,5)) = ".html") OR (Lcase(right(FileName,4)) = ".asp"))then
if (strListofFiles = "") then
strListofFiles = sPath + FileName
else
strListofFiles = strListofFiles + "," + sPath + FileName
end if
end if
Next

' Check all the subfolders for ".htm" & ".html" files
For Each Folder in oFolders
if (Folder.Name <> "images" AND Folder.Name <> "stats" AND Folder.Name <> "_vti_cnf") then
call getCompleteFileList(Folder, sPath + Folder.Name)
end if
Next

' Clean up
set File = Nothing
set oFiles = Nothing
set Folder = Nothing
set oFolders = Nothing

end sub

%>
<html>
<!-- Begin Search Result Area -->

<table cellspacing=0 width="100%" border=0>
<tbody>
<tr bgcolor="#F2F2F2">
<td align=middle colspan=4 bgcolor="#F2F2F2">
<div align="left"><b><font face="Tahoma">Search Results</font></b></div>
</td>
</tr>
<tr bgcolor="#F2F2F2">
<td align=middle colspan=4 bgcolor="#FFFFFF">
<div align="left"></div>
<p align="left">
<font face="Tahoma">
</font>
</p>
<table width="95%" border="0" cellspacing="1" cellpadding="1" align="center">
<tr bgcolor="#330099">
<td bgcolor="#FFFFFF">
<div align="left"><font color="#FFFFFF" face="Tahoma"><b> <font color="#666666">You
searched for</font><font color="#000000"> </font></b><font color="#FF0000"><b><%= strKeyWord%></b></font></font></div>
</td>
</tr>
<%
' Only to the search if we got at least one keyword
if (strKeyWord <> "") then

' Call the sub that gets all the files
call getCompleteFileList(objFolder, "")

' Take the big ole string the contains all the files seperated by commas and chop it up.
arrFileList = split(strListofFiles,",")

For i=0 to UBound(arrFileList)

' Open the current file and read everything into a single string.
' If the files are huge this could eat a lot or resources.
Set objCheckFile = objFS.OpenTextFile(strSitePath + "/" + arrFileList(i),1,false,0)
strFileContents = objCheckFile.ReadAll

' Default the search flag to false.
bKeyWordFound = false

' For each keyword entered search the file.
for j = 0 to UBound(arrKeyWords)
returnValue = InStr(1,strFileContents,arrKeyWords(j),1)

' If the returnValue isn't zero the keyword was found
' Set the search flage to true and short cut out of the loop.
if (returnValue <> 0) then
bKeyWordFound = true
j = UBound(arrKeyWords)
end if
next

' If a keyword was found include this file in the list.
if (bKeyWordFound) then

' Keep track of the number of results and grab the title of the page for the search display
iReturnCount = iReturnCount + 1

dim startChar, endChar
startChar = InStr(1,strFileContents,"<title>",1)
endChar = InStr(1,strFileContents,"</title>",1)

if (startChar = 0 OR endChar = 0 OR ((startChar + 7) > (endChar - 7))) then
%>
<tr>
<td><a href="http://whqs505/HROnline/HROnline.asp?url=Content/Public/<%= arrFileList(i) %>"><font face="Tahoma" size="2"><%= arrFileList(i) %></font></a> <font face="Arial, Helvetica, sans-serif" size="2"> <%= arrFileList(i) %></font></td>
</tr>
<%
else
strPageTitle = mid(strFileContents,InStr(1,strFileContents,"<title>",1) + 7, InStr(1,strFileContents,"</title>",1) - InStr(1,strFileContents,"<title>",1) - 7)

if (strPageTitle = "") then
strPageTitle = arrFileList(i)
end if
%>
<tr>
<td><a href="http://whqs505/HROnline/HROnline.asp?url=Content/Public/<%= arrFileList(i) %>"><font face="Tahoma" size="2"><b><%= strPageTitle%></b></font></a> <br><font face="Arial, Helvetica, sans-serif" size="2"> <%= arrFileList(i) %></font></td>
</tr>
<%
end if
end if

' Close the file so we can open another one.
objCheckFile.Close
Next

end if
%>
<tr>
<td><font face="Tahoma" size="2" color="#999999"><%= iReturnCount%> pages found</font></td>
</tr>
<tr>
<td><font face="Tahoma">&nbsp;</font></td>
</tr>
<%

' Clean up all the objects used.
set objCheckFile = Nothing
set File = Nothing
set objFiles = Nothing
set objFolder = Nothing
set objFS = Nothing

%>
</table>
<!-- the end of the search html -->
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>

whammy
10-24-2002, 01:25 AM
Hmm... without using a regular expression to parse out the meta tag in each file you're reading... that's kinda tough. (And to do that I'd have to devote a bit of research and testing myself, although it DOES seem doable - I just don't honestly have the time to mess with something like that unless I'm getting paid for it!)

That's probably why noone has answered this post... although Mhtml was attempting a couple of weeks ago to do the exact same thing. Perhaps he has found a solution?

Perhaps you can figure out a regular expression to parse the META tag and get the keyword. ;)

Roy Sinclair
10-24-2002, 02:20 PM
Just a question but if you're running on a system with IIS providing ASP, why not use Index Server too. Then you don't need to parse each page, Index Server will do that. All you need to do then is create the IDQ and HTX pages or use ASP and the SQL syntax to get your search results.

biggs27
10-24-2002, 05:35 PM
Whammy,

Thanks for the feedback. I'm glad to know it's possible. I'm going to take a shot at parsing the meta tag. You may see another post here soon! ;)

Roy,

Thanks to you too. I guess I have to air my ignorance here and say that I'm not at a level to completely understand what you're suggesting I do. I am definitely running on IIS with ASP, but beyond that I'm not too sure what all is entailed.

Roy Sinclair
10-24-2002, 07:04 PM
If you have control ov the server running IIS you can install the Index Server product (if it's not installed already, it often is).

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnproasp2/html/overviewofindexserver.asp?frame=true
for more information.

As a nice side benefit you can also index DOC files and other files types. You can even download a filter for Index Server which will index the contents of PDF files.

whammy
10-24-2002, 11:36 PM
Hmm, interesting. I'll have to look into that at work. :)

biggs27
10-25-2002, 04:40 PM
Okay, I've been working away at parsing the data... here's what I came up with.

strPageDescription = mid(strFileContents,(InStr(strFileContents,"Description")+22), InStr(1,strFileContents,"enddescription",1) - InStr(1,strFileContents,"Description",1) - 22)

This gives me the description, but requires me to add "enddescription" to the end of the content of the meta tag. I had to do this to designate an ending place.

This works, but I don't feel like it's the strongest or most foolproof solution. Any suggestions? or is it good as is?

Thanks,
Brian

whammy
10-26-2002, 04:33 AM
Does it work? :D

Although I think personally I'd look for the ending bracket... ">".

My point though, is Bruce Li Programming. If it works, use it. ;)

biggs27
10-29-2002, 07:05 PM
It does work (which seems nothing short of a miracle :D). I tried to make it look for the end bracket, but always get an error message about the "mid" statement. Not sure if there's a better way to find it, but if not I'll keep on using the "enddescription" thing.

Thanks for all of your help!!!

Brian