脚本宝典收集整理的这篇文章主要介绍了ASP中FSO的神奇功能 - 使用FSO进行搜索,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
作 者 : 甘冀平
你也许想:好,现在我
知道如何写入文件了。但能
做到更多一些吗?下面来试一试为web站点建立一个搜索功能。
建立搜索引擎的关键是递归。主要地,编写一段代码搜索目录下的文件,然后对所有的目录循环执行同样的代码。因
为不能确定总共有多少个子目录,所以必须一遍又一遍地执行搜索代码,直到结束。
递归调用非常好!
下面来创建搜索页面。假设已经建立了一个HT
ML表单,用户在其中输入一个搜索字符串。
Dim objFolder
Dim strSe
ArchText
Dim objFSO
strSearchText = Request.Form("SearchText") < -- The search string
' create the FSO and Folder objects
Set fso = Server.CreateObject("Scripting.File
SystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))
Search objFolder
上面的代码
简单地初始化变量,Search函数执行搜索功能,描述如下:
Function Search(objFolder)
Dim objS
ubFolder
'loop t
hrough every file in the
current
folder
For
each objFile in objFolder.Files
Set objTextStre
am = objFSO.O
PEnTextFile(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
wr
ITe a link
' to the file
If InStr(1, strFileContents, strSearchText, 1) then
Response.Write "< A HREF=""/" &am
p; 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 ag
ain
For Each objSubFolder in objFolder.SubFolders
Search objSubFolder
Next
End Function
为了能打开文件,FSO需要实际的文件路径,而不是web路径。比如,是c:
inetpubwwwroot empindex.html, 而不是
www.enfused
.COM/temp/index.html
或者 /temp/index.html。 为了将后者转换为前者,使用Server.MapPath
("filename"), filename表示web路径名。
上面的代码将在你指定的初始目录下的文件夹的每一个子目录中执行,在这里,初始目录是指web根目录“/”。然后
就简单地打开目录下的每一个文件,看看其中
是否包含指定的字符串,如果找到字符串就显示那个文件的链接。
注意,随着文件和子目录数量的增加,搜索花费的时间也将增加。如果需要繁重的搜索工作,建议你采取其他的方
法,比如
微软公司的索引
服务器Index Server。
脚本宝典总结
以上是脚本宝典为你收集整理的ASP中FSO的神奇功能 - 使用FSO进行搜索全部内容,希望文章能够帮你解决ASP中FSO的神奇功能 - 使用FSO进行搜索所遇到的问题。
如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。