如何做一个文本搜索?
<%
head = "搜索"
searchstring = request("searchstring")
count=0
function unmappath( path )
unmappath = replace(mid(path, len(server.mappath("/")) + 1), "\", "/")
' 把当前目录的实际路径转换为虚拟路径.
end function
function searchfile( f, s, title )
set fo = fs.opentextfile(f)
content = fo.readall
' 把全部文本读到content.
fo.close
searchfile = instr(1, content, s, vbtextcompare) > 0
' 从第一个字符开始检查content里面是否有s.
if searchfile then
' 如果有,则提出文件title存入变量.
pos1 = instr(1, content, "<title>", vbtextcompare)
pos2 = instr(1, content, "</title>", vbtextcompare)
title = ""
if pos1 > 0 and pos2 > 0 then
' 取title标记中间的字符.
title = mid( content, pos1 + 7, pos2 - pos1 - 7 )
end if
end if
end function
function filelink( f, title )
vpath = unmappath( f.path )
' 获取路径.
if title = "" then title = f.name
' 做个链接.
filelink = "<a href=""" & vpath & """>" & title & "</a>"
filelink = "<ul>·" & filelink & "</ul>"
end function
sub searchfolder( fd, s )
found = false
for each f in fd.files
pos = instrrev(f.path, "." )
if pos > 0 then
ext = mid(f.path, pos + 1 )
else
ext = ""
end if
if lcase(ext) = "htm" then
' 显示扩展名字为htm的文件.
if searchfile( f, s, title ) then
response.write filelink(f, title)
count=count+1
response.write cstr(count)
end if
end if
next
for each sfd in fd.subfolders
searchfolder sfd, s
next
end sub
%>
<html>
<head>
<meta http-equiv="content-type"
content="text/html; charset=gb_2312-80">
<meta name="generator" content="microsoft frontpage express 2.0">
<title><%=head%></title>
</head>
<body>
<h1>星河影动之无敌文本搜索<%=head%></h1>
<hr>
<form action="search.asp" method="get">
<p>请输入想要搜索的内容: <input type="text"
size="20" name="searchstring" value="<%=searchstring%>"> <input
type="submit" value="搜索"> </p>
</form>
<%
set fs = server.createobject("scripting.filesystemobject")
set fd = fs.getfolder( server.mappath("/") )
' 设置开始搜索的路径.
if searchstring <> "" then
response.write "<h2>搜索<font color=red>" & searchstring & "</font>结果如下:</h2><p>"
searchfolder fd,searchstring
end if
%>
<hr>
</body></html>
[1]