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

如何使用FSO搜索硬盘文件

程序员文章站 2022-07-02 07:53:55
<%@language="vbscript" codepage="936"%> <% dim st st=t...


<%@language="vbscript" codepage="936"%>
<%
dim st
st=timer()
'*************************************************************
'*************搜索硬盘文件的类searchfile *************
'*************调用方法: *************
'*************set newsearch=new searchfile '声明 *************
'*************newsearch.folder="f:+e:"'传入搜索源*************
'*************newsearch.keyword="汇编" '关键词*************
'*************newsearch.search '开始搜索*************
'*************set newsearch=nothing '结束*************
'*************************************************************
class searchfile
dim folders '传入绝对路径,多路径使用+号连接,不能有空格
dim keyword '传入关键词
dim objfso '定义全局变量
dim counter '定义全局变量,搜索结果的数目
'*****************初始化**************************************
private sub class_initialize
set objfso=server.createobject("scripting.filesystemobject")
counter=0 '初始化计数器
end sub
'************************************************************
private sub class_terminate
set objfso=nothing
end sub
'**************公有成员,调用的方法***************************
function search
folders=split(folders,"+") '转化为数组
keyword=trim(keyword) '去掉前后空格
if keyword="" then
response.write("<font color='red'>关键字不能为空</font><br/>")
exit function
end if
'判断是否包含非法字符
flag=instr(keyword,"") or instr(keyword,"/")
flag=flag or instr(keyword,":")
flag=flag or instr(keyword,"|")
flag=flag or instr(keyword,"&")

if flag then '关键字中不能包含/:|&
response.write("<font color='red'>关键字不能包含/:|&</font><br/>")
exit function '如果包含有这个则退出
end if
'多路径搜索
dim i
for i=0 to ubound(folders)
call getallfile(folders(i)) '调用循环递归函数
next
response.write("共搜索到<font color='red'>"&counter&"</font>个结果")
end function
'***************历遍文件和文件夹******************************
private function getallfile(folder)
dim objfd,objfs,objff
set objfd=objfso.getfolder(folder)
set objfs=objfd.subfolders
set objff=objfd.files
'历遍子文件夹
dim strfdname '声明子文件夹名
'*********历遍子文件夹******
on error resume next
for each onedir in objfs
strfdname=onedir.name
'系统文件夹不在历遍之列
if strfdname<>"config.msi" eqv strfdname<>"recycled" eqv strfdname<>"recycler" eqv strfdname<>"system volume information" then
sfn=folder&""&strfdname '绝对路径
call getallfile(sfn) '调用递归
end if
next
dim strflname
'**********历遍文件********
for each onefile in objff
strflname=onefile.name
'desktop.ini和folder.htt不在列取范围
if strflname<>"desktop.ini" eqv strflname<>"folder.htt" then
fn=folder&""&strflname
counter=counter+coloron(fn)
end if
next
'***************************
'关闭各对象实例
set objfd=nothing
set objfs=nothing
set objff=nothing
end function
'*********************生成匹配模式***********************************
private function createpattern(keyword)
createpattern=keyword
createpattern=replace(createpattern,".",".")
createpattern=replace(createpattern,"+","+")
createpattern=replace(createpattern,"(","(")
createpattern=replace(createpattern,")",")")
createpattern=replace(createpattern,"[","[")
createpattern=replace(createpattern,"]","]")
createpattern=replace(createpattern,"{","{")
createpattern=replace(createpattern,"}","}")
createpattern=replace(createpattern,"*","[^\/]*") '*号匹配
createpattern=replace(createpattern,"?","[^\/]{1}") '?号匹配
createpattern="("&createpattern&")+" '整体匹配
end function
'**************************搜索并使关键字上色*************************
private function coloron(filename)
dim objreg
set objreg=new regexp
objreg.pattern=createpattern(keyword)
objreg.ignorecase=true
objreg.global=true
retval=objreg.test(filename) '进行搜索测试,如果通过则上色并输出
if retval then
output=objreg.replace(filename,"<font color='#ff0000'>$1</font>") '设置关键字的显示颜色
'***************************该部分可以根据需要修改输出************************************
output="<a href='#'>"&output&"</a><br/>"
response.write(output) '输出匹配的结果
'*************************************可修改部分结束**************************************
coloron=1 '加入计数器的数目
else
coloron=0
end if
set objreg=nothing
end function
end class
'************************结束类searchfile**********************
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>www.csdn.com.cn</title>
</head>

<body>
<form name="form1" method="post" action="<% =request.servervariables("path_info")%>">
关键词:
<input name="keyword" type="text" id="keyword">
<input type="submit" name="submit" value="搜索">
<a href="help.htm" target="_blank">高级搜索帮助</a>
</form>
<%
dim keyword
keyword=request.form("keyword")
if keyword<>"" then
set newsearch=new searchfile
newsearch.folders="e:media+f:"
newsearch.keyword=keyword
newsearch.search
set newsearch=nothing
response.write("<br/>费时:"&(timer()-st)*1000&"毫秒")
end if

%>
</body>
</html>