我的人生路  
          日歷
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789
          統計
          • 隨筆 - 74
          • 文章 - 57
          • 評論 - 7
          • 引用 - 0

          導航

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          顏色

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          你也許想:好,現在我知道如何寫入文件了。但能做到更多一些嗎?下面來試一試為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。



          歡迎大家訪問我的個人網站 萌萌的IT人
          posted on 2005-07-01 16:49 一天一點愛戀 閱讀(114) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          Copyright © 一天一點愛戀 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 汉川市| 东宁县| 太仆寺旗| 石狮市| 柘城县| 金乡县| 苏州市| 潮州市| 鲜城| 青河县| 即墨市| 张家港市| 镇江市| 大同市| 永清县| 天气| 贵阳市| 南澳县| 绥宁县| 五大连池市| 缙云县| 西和县| 含山县| 同德县| 德化县| 德令哈市| 茌平县| 金昌市| 河津市| 原阳县| 台东县| 塔河县| 峡江县| 定边县| 汉沽区| 运城市| 沁源县| 阳原县| 油尖旺区| 昌吉市| 广宗县|