ASP中FSO的神奇功能 - 使用FSO進行搜尋

來源:互聯網
上載者:User
作 者 : 甘冀平
   你也許想:好,現在我知道如何寫入檔案了。但能做到更多一些嗎?下面來試一試為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, 而不是

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.