快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

ASP中FSO的神奇功能 - 使用FSO进行搜索

你也许想:好,现在我知道如何写入文件了。但能做到更多一些吗?下面来试一试为web站点建立一个搜索功能。

   建立搜索引擎的要害是递归。主要地,编写一段代码搜索目录下的文件,然后对所有的目录循环执行同样的代码。因
为不能确定总共有多少个子目录,所以必须一遍又一遍地执行搜索代码,直到结束。递归调用异常好!

   下面来创建搜索页面。假设已经建立了一个HTML表单,用户在其中输入一个搜索字符串。

Dim objFolder
Dim strSearchText
Dim objFSO

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

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系列讲座(十四)包含文件
下一教程: ASP常见问题(新手)

您可以阅读与"ASP中FSO的神奇功能 - 使用FSO进行搜索"相关的教程:
· ASP中FSO的神奇功能 - 用FSO进行内容治理
· ASP中FSO的神奇功能 - 权限许可
· ASP中FSO的神奇功能 - 写文件
· ASP中FSO的神奇功能
· ASP中FSO的神奇功能 - 文件读取
    微笑服务 优质保证 索取样品