⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0122.htm

📁 ASP教程宝典 书籍语言: 简体中文 书籍类型: 网络编程 授权方式: 免费软件 书籍大小: 500 KB
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
    <p align="center"><big><strong>使用FSO进行搜索</strong></big></p>

<div align="right">---(文/甘冀平)</div>

你也许想:好,现在我知道如何写入文件了。但能做到更多一些吗?下面来试一试为web站点建立一个搜索功能。建立搜索引擎的关键是递归。主要地,编写一段代码搜索目录下的文件,然后对所有的目录循环执行同样的代码。因为不能确定总共有多少个子目录,所以必须一遍又一遍地执行搜索代码,直到结束。递归调用非常好!下面来创建搜索页面。假设已经建立了一个HTML表单,用户在其中输入一个搜索字符串。<br>
<br>
Dim objFolder<br>
Dim strSearchText<br>
Dim objFSO<br>
<br>
strSearchText = Request.Form(&quot;SearchText&quot;)&nbsp;&nbsp;&lt; -- The search string<br>
' create the FSO and Folder objects<br>
Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br>
Set objFolder = objFSO.GetFolder(Server.MapPath(&quot;/&quot;))<br>
<br>
Search objFolder<br>
<br>
<br>
   上面的代码简单地初始化变量,Search函数执行搜索功能,描述如下:<br>
<br>
<br>
<br>
Function Search(objFolder)<br>
<br>
&nbsp;&nbsp;Dim objSubFolder<br>
<br>
<br>
<br>
&nbsp;&nbsp;'loop through every file in the current<br>
folder<br>
<br>
&nbsp;&nbsp;For Each objFile in objFolder.Files<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set objTextStream = objFSO.OpenTextFile(objFile.Path,1) &lt; -- For Reading<br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'read the file's contents into a<br>
variable<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strFileContents = objTextStream.ReadAll<br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'if the search string is in the file, then<br>
write a link<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' to the file<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If InStr(1, strFileContents, strSearchText, 1) then<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write &quot;&lt; A HREF=&quot;&quot;/&quot; &amp; objFile.Name &amp; _<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot;&gt;&quot; &amp; objFile.Name &amp; &quot;&lt; /A&gt;&lt; BR&gt;&quot;<br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bolFileFound = True<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objTextStream.Close<br>
<br>
<br>
<br>
&nbsp;&nbsp;Next<br>
<br>
<br>
<br>
&nbsp;&nbsp;'Here's the recursion part - for each<br>
<br>
&nbsp;&nbsp;' subfolder in this directory, run the Search function again<br>
<br>
&nbsp;&nbsp;For Each objSubFolder in objFolder.SubFolders<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Search objSubFolder<br>
<br>
&nbsp;&nbsp;Next<br>
<br>
End Function<br>
<br>
   为了能打开文件,FSO需要实际的文件路径,而不是web路径。比如,是c:inetpubwwwroot empindex.html, 而不是 <br>
www.enfused.com/temp/index.html 或者 /temp/index.html。 为了将后者转换为前者,使用Server.MapPath<br>
(&quot;filename&quot;), filename表示web路径名。<br>
<br>
   上面的代码将在你指定的初始目录下的文件夹的每一个子目录中执行,在这里,初始目录是指web根目录“/”。然后<br>
就简单地打开目录下的每一个文件,看看其中是否包含指定的字符串,如果找到字符串就显示那个文件的链接。<br>
<br>
   注意,随着文件和子目录数量的增加,搜索花费的时间也将增加。如果需要繁重的搜索工作,建议你采取其他的方<br>
法,比如微软公司的索引服务器Index Server。

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -