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

2016-01-29 19:05 10 1 收藏

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

【 tulaoshi.com - ASP 】

  平

   你也许想:好,现在我知道如何写入文件了。但能做到更多一些吗?下面来试一试为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。
 

来源:https://www.tulaoshi.com/n/20160129/1508415.html

延伸阅读
标签: ASP
      FSO中有个方法是CreateFolder,但是这个方法只能在其上一级文件夹存在的情况下创建新的文件夹,所以我就写了一个自动创建多级文件夹的函数,在生成静态页面等方面使用非常方便. 函数:   ' -------------------------------- ' 自动创建指定的多级文件夹 ' strPath为绝对路径 ' 引用请保留版权 ' by im286_Anjer...
标签: ASP
  '共有两个页面index.htm是首页.程序比较简单,主要是用了ASP里的文件操作对象。 <!--index.htm------------------------------------------------------------------------------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" <html <head <titleUntitl...
标签: ASP
  下面列举一下这些不常用但是却非常酷的功能: 很少被了解的FSO功能 GetSpecialFolder Method 返回特定的Windows文件夹的路径: Windows安装目录;Windows系统目录;Windows临时目录 FSO.GetSpecialFolder([0, 1, or 2]) GetTempName Method 返回一个随机产生的文件或者目录名字,用于需要存储临时数据时 GetAbsolutePathName...
标签: Web开发
试了一下,xmlDoc.save()行不同,就试着用fso做了出来。 整理一下,供大家discuss。 由于用js操作本地xml文件之后save时会permission denied,所以据我所知就只能通过FSO来做。而用FSO,基本上就与文件是否是xml无关。 xml文件: root parent child1one/child1 child2one/child2 /parent parent child1two/child1 child2two/child2 /parent ...
标签: ASP
<%@ Language=VBScript % <%Server.ScriptTimeout=50000% <HTML <HEAD <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" <meta http-equiv="Content-Type" content="text/html; charset=gb2312" </HEAD <BODY <% function JudgeParaRegular(intID) if intID<"" and isnumeric(intID) then JudgePar...

经验教程

758

收藏

28
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部