strSearchText = Request.Form("SearchText") -- The search string
' create the FSO and Folder objects
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))
Search objFolder
上面的代码简单地初始化变量,Search函数执行搜索功能,描述如下:
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