This browser does not support basic Web standards, preventing the display of our site's intended design. May we suggest that you upgrade your browser?

Web Development Code Library ASPSimple site search

Simple site search

You can search through HTML, ASP, and text documents to match a search string. Can be called from another page.

'Assumes you have posted data to this page using a Text field with the name "SearchText"

<%
Dim objFolder
Dim strSearchText
Dim objFSO

strSearchText = Request.Form("SearchText")

' create the FSO and Folder objects

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))

Search objFolder

Function Search(objFolder)

     Dim objSubFolder

     'loop through every file in the current folder

     For Each objFile in objFolder.Files
  
          Set objTextStream = objFSO.OpenTextFile(objFile.Path,1) <-- For Reading

        'read the file's contents into a variable
        strFileContents = objTextStream.ReadAll

        'if the search string is in the file, then write a link to the file
        If InStr(1, strFileContents, strSearchText, 1) then
        
           Response.Write "<A HREF=""/" & objFile.Name & """>" & objFile.Name & "</A><BR>"
           bolFileFound = True
           
        End If

        objTextStream.Close
     Next

  'Here's the recursion part - for each subfolder in this directory, run the Search function again

     For Each objSubFolder in objFolder.SubFolders
          Search objSubFolder
     Next

End Function

%>
Excellence. Our Measure. Our Motto. Our Goal.