国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > ASP > 正文

ASP中FSO的神奇功能 - 使用FSO進(jìn)行搜索

2024-05-04 11:07:43
字體:
供稿:網(wǎng)友
你也許想:好,現(xiàn)在我知道如何寫入文件了。但能做到更多一些嗎?下面來試一試為web站點(diǎn)建立一個(gè)搜索功能。

   建立搜索引擎的關(guān)鍵是遞歸。主要地,編寫一段代碼搜索目錄下的文件,然后對(duì)所有的目錄循環(huán)執(zhí)行同樣的代碼。因
為不能確定總共有多少個(gè)子目錄,所以必須一遍又一遍地執(zhí)行搜索代碼,直到結(jié)束。遞歸調(diào)用非常好!

   下面來創(chuàng)建搜索頁面。假設(shè)已經(jīng)建立了一個(gè)HTML表單,用戶在其中輸入一個(gè)搜索字符串。

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函數(shù)執(zhí)行搜索功能,描述如下:



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

   為了能打開文件,F(xiàn)SO需要實(shí)際的文件路徑,而不是web路徑。比如,是c:inetpubwwwroot empindex.html, 而不是
www.enfused.com/temp/index.html 或者 /temp/index.html。 為了將后者轉(zhuǎn)換為前者,使用Server.MapPath
("filename"), filename表示web路徑名。

   上面的代碼將在你指定的初始目錄下的文件夾的每一個(gè)子目錄中執(zhí)行,在這里,初始目錄是指web根目錄“/”。然后
就簡單地打開目錄下的每一個(gè)文件,看看其中是否包含指定的字符串,如果找到字符串就顯示那個(gè)文件的鏈接。

   注意,隨著文件和子目錄數(shù)量的增加,搜索花費(fèi)的時(shí)間也將增加。如果需要繁重的搜索工作,建議你采取其他的方
法,比如微軟公司的索引服務(wù)器Index Server。  
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大名县| 建宁县| 平塘县| 尉氏县| 岑巩县| 镇坪县| 吐鲁番市| 庆阳市| 石泉县| 额济纳旗| 临安市| 凯里市| 韩城市| 保靖县| 双牌县| 乡城县| 东乌| 楚雄市| 班戈县| 湖州市| 博野县| 祁连县| 石棉县| 清徐县| 新晃| 蓬溪县| 博爱县| 岳池县| 尖扎县| 宁蒗| 忻州市| 宿州市| 平顺县| 榆树市| 洪洞县| 攀枝花市| 化隆| 迁安市| 如皋市| 罗田县| 巴南区|