欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

文本搜索

程序员文章站 2022-05-28 16:40:34
<% head = "搜索" searchstring = request("searchstring") count=...
<%
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 bgcolor="#ffffff">

<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>