当前位置:K88软件开发文章中心编程语言AspAsp01 → 文章内容

如何使用FSO搜索硬盘文件

减小字体 增大字体 作者:     来源:asp编程网  发布时间:2018-12-30 8:29:39



<
%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<
%
dim st
st=timer()
&apos
*************************************************************
&apos
*************搜索硬盘文件的类SearchFile *************
&apos
*************调用方法: *************
&apos
*************Set newsearch=new SearchFile &apos
声明 *************
&apos
*************newsearch.Folder="F:+E:"&apos
传入搜索源*************
&apos
*************newsearch.keyword="汇编" &apos
关键词*************
&apos
*************newsearch.Search &apos
开始搜索*************
&apos
*************Set newsearch=Nothing &apos
结束*************
&apos
*************************************************************
Class SearchFile
dim Folders &apos
传入绝对路径,多路径使用+号连接,不能有空格
dim keyword &apos
传入关键词
dim objFso &apos
定义全局变量
dim Counter &apos
定义全局变量,搜索结果的数目
&apos
*****************初始化**************************************
Private Sub Class_Initialize
Set objFso=Server.CreateObject("Scripting.FileSystemObject")
Counter=0 &apos
初始化计数器
End Sub
&apos
************************************************************
Private Sub Class_Terminate
Set objFso=Nothing
End Sub
&apos
**************公有成员,调用的方法***************************
Function Search
Folders=split(Folders,"+") &apos
转化为数组
keyword=trim(keyword) &apos
去掉前后空格
if keyword="" then
Response.Write("red&apos
>关键字不能为空

")
exit Function
end if
&apos
判断是否包含非法字符
flag=instr(keyword,"") or instr(keyword,"/")
flag=flag or instr(keyword,":")
flag=flag or instr(keyword,"|")
flag=flag or instr(keyword,"&")

if flag then &apos
关键字中不能包含/:|&
Response.Write("red&apos
>关键字不能包含/:|&

")
Exit Function &apos
如果包含有这个则退出
end if
&apos
多路径搜索
dim i
for i=0 to ubound(Folders)
Call GetAllFile(Folders(i)) &apos
调用循环递归函数
next
Response.Write("共搜索到red&apos
>"&Counter&"
个结果")
End Function
&apos
***************历遍文件和文件夹******************************
Private Function GetAllFile(Folder)
dim objFd,objFs,objFf
Set objFd=objFso.GetFolder(Folder)
Set objFs=objFd.SubFolders
Set objFf=objFd.Files
&apos
历遍子文件夹
dim strFdName &apos
声明子文件夹名
&apos
*********历遍子文件夹******
on error resume next
For Each OneDir In objFs
strFdName=OneDir.Name
&apos
系统文件夹不在历遍之列
If strFdName<>"Config.Msi" EQV strFdName<>"RECYCLED" EQV strFdName<>"RECYCLER" EQV strFdName<>"System Volume Information" Then
SFN=Folder&""&strFdName &apos
绝对路径
Call GetAllFile(SFN) &apos
调用递归
End If
Next
dim strFlName
&apos
**********历遍文件********
For Each OneFile In objFf
strFlName=OneFile.Name
&apos
desktop.ini和folder.htt不在列取范围
If strFlName<>"desktop.ini" EQV strFlName<>"folder.htt" Then
FN=Folder&""&strFlName
Counter=Counter+ColorOn(FN)
End If
Next
&apos
***************************
&apos
关闭各对象实例
Set objFd=Nothing
Set objFs=Nothing
Set objFf=Nothing
End Function
&apos
*********************生成匹配模式***********************************
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,"*","[^/]*") &apos
*号匹配
CreatePattern=Replace(CreatePattern,"?","[^/]
{1}") &apos
?号匹配
CreatePattern="("&CreatePattern&")+" &apos
整体匹配
End Function
&apos
**************************搜索并使关键字上色*************************
Private Function ColorOn(FileName)
dim objReg
Set objReg=new RegExp
objReg.Pattern=CreatePattern(keyword)
objReg.IgnoreCase=True
objReg.Global=True
retVal=objReg.Test(FileName) &apos
进行搜索测试,如果通过则上色并输出
if retVal then
OutPut=objReg.Replace(FileName,"
#FF0000&apos
>$1
") &apos
设置关键字的显示颜色
&apos
***************************该部分可以根据需要修改输出************************************
OutPut="
#&apos
>"&OutPut&"

"
Response.Write(OutPut) &apos
输出匹配的结果
&apos
*************************************可修改部分结束**************************************
ColorOn=1 &apos
加入计数器的数目
else
ColorOn=0
end if
Set objReg=Nothing
End Function
End Class
&apos
************************结束类SearchFile**********************
%>




www.csdn.com.cn



">
关键词:


高级搜索帮助

<
%
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("
费时:"&(timer()-st)*1000&"毫秒")
end if

%>








如何使用FSO搜索硬盘文件