知识库

一种常见的ASP搜索硬盘文件的代码

来源: 本站    类别: 知识库    日期: 2013/5/2

 可能具有一定的危害性,请不要用于非法企图,否则后果自负
<%
'**************************代码源自网络***********************
'******************可能具有一定的危害性,请不要用于非法企图,否则后果自负*******************
'**********************修改:Build2013 ***********************
'*************Setnewsearch=newSearchFile'声明*************
'*************newsearch.Folder="F:+E:"'传入搜索源*************
'*************newsearch.keyword="汇编"'关键词*************
'*************newsearch.Search'开始搜索*************
'*************Setnewsearch=Nothing'结束*************
'*************Source From www.cnhww.com*********************
Server.ScriptTimeOut=99999'程序加载的超时设置
ClassSearchFile
dimFolders'传入绝对路径,多路径使用+号连接,不能有空格
dimkeyword'传入关键词
dimobjFso'定义全局变量
dimCounter'定义全局变量,搜索结果的数目
'*****************初始化**************************************
PrivateSubClass_Initialize
 SetobjFso=Server.CreateObject("Scripting.FileSystemObject")
 Counter=0'初始化计数器
EndSub
'************************************************************
PrivateSubClass_Terminate
 SetobjFso=Nothing
EndSub
'**************公有成员,调用的方法***************************
FunctionSearch
 Folders=split(Folders,"+")'转化为数组
 keyword=trim(keyword)'去掉前后空格
 ifkeyword=""then
 Response.Write("<fontcolor='red'>关键字不能为空</font><br/>")
exitFunction
 endif
 '判断是否包含非法字符
 flag=instr(keyword,"")orinstr(keyword,"/")
 flag=flagorinstr(keyword,":")
 flag=flagorinstr(keyword,"|")
 flag=flagorinstr(keyword,"&")
 
 ifflagthen'关键字中不能包含/:|&
 Response.Write("<fontcolor='red'>关键字不能包含/:|&</font><br/>")
ExitFunction'如果包含有这个则退出
 endif
 '多路径搜索
 dimi
 fori=0toubound(Folders)
 CallGetAllFile(Folders(i))'调用循环递归函数
 next
 Response.Write("共搜索到<fontcolor='red'>"&Counter&"</font>个结果")
EndFunction
'***************历遍文件和文件夹******************************
PrivateFunctionGetAllFile(Folder)
 dimobjFd,objFs,objFf
 SetobjFd=objFso.GetFolder(Folder)
 SetobjFs=objFd.SubFolders
 SetobjFf=objFd.Files
 '历遍子文件夹
 dimstrFdName'声明子文件夹名
 '*********历遍子文件夹******
 onerrorresumenext
 ForEachOneDirInobjFs
 strFdName=OneDir.Name
'系统文件夹不在历遍之列
 IfstrFdName<>"Config.Msi"EQVstrFdName<>"RECYCLED"EQVstrFdName<>"RECYCLER"EQVstrFdName<>"SystemVolumeInformation"Then
 SFN=Folder&""&strFdName'绝对路径
 CallGetAllFile(SFN)'调用递归
EndIf
 Next
 dimstrFlName
 '**********历遍文件********
 ForEachOneFileInobjFf
 strFlName=OneFile.Name
'desktop.ini和folder.htt隐藏的系统文件不在列取范围
 IfstrFlName<>"desktop.ini"EQVstrFlName<>"folder.htt"Then
 FN=Folder&""&strFlName
 Counter=Counter+ColorOn(FN)
EndIf
 Next
 '***************************
 '关闭各对象实例
 SetobjFd=Nothing
 SetobjFs=Nothing
 SetobjFf=Nothing
EndFunction
'*********************生成匹配模式***********************************
PrivateFunctionCreatePattern(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&")+"'整体匹配
EndFunction
'**************************搜索并使关键字上色*************************
PrivateFunctionColorOn(FileName)
 dimobjReg
 SetobjReg=newRegExp
 objReg.Pattern=CreatePattern(keyword)
 objReg.IgnoreCase=True
 objReg.Global=True
 retVal=objReg.Test(FileName)'进行搜索测试,如果通过则上色并输出
 ifretValthen
 OutPut=objReg.Replace(FileName,"<fontcolor='#FF0000'>$1</font>")'设置关键字的显示颜色
'***************************该部分可以根据需要修改输出************************************
 OutPut="<ahref='#'>"&OutPut&"</a><br/>"
 Response.Write(OutPut)'输出匹配的结果
'*************************************可修改部分结束**************************************
 ColorOn=1'加入计数器的数目
 else
 ColorOn=0
 endif
 SetobjReg=Nothing
EndFunction

相关文章


Copyright © 2004 - 2024 CNHWW Inc. All Rights Reserved
石家庄市征红网络科技有限公司版权所有 邮政编码:050051
服务电话:0311-85315152 13931185013 在线客服QQ:81447932 / 81447933 邮箱: cnhww@163.com