如何使用FSO搜索硬盘文件

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

 1   
 2dim st   
 3st=timer()   
 4'*************************************************************   
 5'*************搜索硬盘文件的类SearchFile *************   
 6'*************调用方法: *************   
 7'*************Set newsearch=new SearchFile '声明 *************   
 8'*************newsearch.Folder="F:+E:"'传入搜索源*************   
 9'*************newsearch.keyword="汇编" '关键词*************   
10'*************newsearch.Search '开始搜索*************   
11'*************Set newsearch=Nothing '结束*************   
12'*************************************************************   
13Class SearchFile   
14dim Folders '传入绝对路径,多路径使用+号连接,不能有空格   
15dim keyword '传入关键词   
16dim objFso '定义全局变量   
17dim Counter '定义全局变量,搜索结果的数目   
18'*****************初始化**************************************   
19Private Sub Class_Initialize   
20Set objFso=Server.CreateObject("Scripting.FileSystemObject")   
21Counter=0 '初始化计数器   
22End Sub   
23'************************************************************   
24Private Sub Class_Terminate   
25Set objFso=Nothing   
26End Sub   
27'**************公有成员,调用的方法***************************   
28Function Search   
29Folders=split(Folders,"+") '转化为数组   
30keyword=trim(keyword) '去掉前后空格   
31if keyword="" then   
32Response.Write("

<font color="red">关键字不能为空</font>

 1")   
 2exit Function   
 3end if   
 4'判断是否包含非法字符   
 5flag=instr(keyword,"\") or instr(keyword,"/")   
 6flag=flag or instr(keyword,":")   
 7flag=flag or instr(keyword,"|")   
 8flag=flag or instr(keyword,"&")   
 9  
10if flag then '关键字中不能包含\/:|&   
11Response.Write("

<font color="red">关键字不能包含/:|&amp;</font>

1")   
2Exit Function '如果包含有这个则退出   
3end if   
4'多路径搜索   
5dim i   
6for i=0 to ubound(Folders)   
7Call GetAllFile(Folders(i)) '调用循环递归函数   
8next   
9Response.Write("共搜索到

<font color="red">"&amp;Counter&amp;"</font>

 1个结果")   
 2End Function   
 3'***************历遍文件和文件夹******************************   
 4Private Function GetAllFile(Folder)   
 5dim objFd,objFs,objFf   
 6Set objFd=objFso.GetFolder(Folder)   
 7Set objFs=objFd.SubFolders   
 8Set objFf=objFd.Files   
 9'历遍子文件夹   
10dim strFdName '声明子文件夹名   
11'*********历遍子文件夹******   
12on error resume next   
13For Each OneDir In objFs   
14strFdName=OneDir.Name   
15'系统文件夹不在历遍之列   
16If strFdName<>"Config.Msi" EQV strFdName<>"RECYCLED" EQV strFdName<>"RECYCLER" EQV strFdName<>"System Volume Information" Then   
17SFN=Folder&"\"&strFdName '绝对路径   
18Call GetAllFile(SFN) '调用递归   
19End If   
20Next   
21dim strFlName   
22'**********历遍文件********   
23For Each OneFile In objFf   
24strFlName=OneFile.Name   
25'desktop.ini和folder.htt不在列取范围   
26If strFlName<>"desktop.ini" EQV strFlName<>"folder.htt" Then   
27FN=Folder&"\"&strFlName   
28Counter=Counter+ColorOn(FN)   
29End If   
30Next   
31'***************************   
32'关闭各对象实例   
33Set objFd=Nothing   
34Set objFs=Nothing   
35Set objFf=Nothing   
36End Function   
37'*********************生成匹配模式***********************************   
38Private Function CreatePattern(keyword)   
39CreatePattern=keyword   
40CreatePattern=Replace(CreatePattern,".","\\.")   
41CreatePattern=Replace(CreatePattern,"+","\\+")   
42CreatePattern=Replace(CreatePattern,"(","\\(")   
43CreatePattern=Replace(CreatePattern,")","\\)")   
44CreatePattern=Replace(CreatePattern,"[","\\[")   
45CreatePattern=Replace(CreatePattern,"]","\\]")   
46CreatePattern=Replace(CreatePattern,"{","\\{")   
47CreatePattern=Replace(CreatePattern,"}","\\}")   
48CreatePattern=Replace(CreatePattern,"*","[^\\\\\/]*") '*号匹配   
49CreatePattern=Replace(CreatePattern,"?","[^\\\\\/]{1}") '?号匹配   
50CreatePattern="("&CreatePattern&")+" '整体匹配   
51End Function   
52'**************************搜索并使关键字上色*************************   
53Private Function ColorOn(FileName)   
54dim objReg   
55Set objReg=new RegExp   
56objReg.Pattern=CreatePattern(keyword)   
57objReg.IgnoreCase=True   
58objReg.Global=True   
59retVal=objReg.Test(FileName) '进行搜索测试,如果通过则上色并输出   
60if retVal then   
61OutPut=objReg.Replace(FileName,"

<font color="#FF0000">$1</font>

1") '设置关键字的显示颜色   
2'***************************该部分可以根据需要修改输出************************************   
3OutPut="

<a href="#">"&amp;OutPut&amp;"</a>

 1"   
 2Response.Write(OutPut) '输出匹配的结果   
 3'*************************************可修改部分结束**************************************   
 4ColorOn=1 '加入计数器的数目   
 5else   
 6ColorOn=0   
 7end if   
 8Set objReg=Nothing   
 9End Function   
10End Class   
11'************************结束类SearchFile**********************   
 1<html>
 2<head>
 3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 4<title>www.knowsky.com</title>
 5</head>
 6<body>
 7<form ```"="" action="```
 8 =Request.ServerVariables(" method="post" name="form1" path_info")="">   
 9关键词:   
10<input id="keyword" name="keyword" type="text"/>
11<input name="Submit" type="submit" value="搜索"/>
12<a href="help.htm" target="_blank">高级搜索帮助</a>
13</form>   

dim keyword
keyword=Request.Form("keyword")
if keyword&lt;&gt;"" then
Set newsearch=new SearchFile
newsearch.Folders="E:\Media+F:"
newsearch.keyword=keyword
newsearch.Search
Set newsearch=Nothing
response.Write(" 费时:"&amp;(timer()-st)*1000&amp;"毫秒")
end if

1</body>
2</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus