网络软件 系统工具 应用软件 图形图像 多媒体类 免费游戏 安全相关 免费音乐 网页素材 电子书籍 考试考题 建站源码
教育教学 多媒体类 编程开发 操作系统 游戏天地 娱乐天地 简历求职 站长专区 网页设计 安全技术 图形图像 文学驿站
业界资讯 | 图形图像 | 操作系统 | 网络冲浪 | 工具软件 | 办公软件 | 媒体动画 | 精文荟萃 | 认证考试 | 网页设计 | 技术开发 | 专栏
当前位置:热点网络学院技术开发ScriptVbScript如何使用FSO搜索硬盘文件
精品推荐
热点TOP10
·Microsoft VBscript运行时错误'800a000d'的解决方案
·验证email地址是否合法完整实例
·判断一个字符串里面包含有中文或者英文
·使用VBScript操作Html复选框(CheckBox)控件
·让文本输入框只能输入数字
·vbscript与javascript传递变量
·URL编码处理
·获取汉字拼音的第一个字母
·EMS图片格式转换(Java)程序
·vbscript错误代码及对应解释大全
·编写高质量的VB代码
·检测IP地址是否真正合法的函数
·JavaScript幻灯片效果
·js入门·循环与判断/利用函数的简单实例/使用对象/列举对象属性的名称
·身份验证常用正则表达式
·如何使用FSO搜索硬盘文件
·为Nokia S60编写移动游戏之二
·在VB中利用Word宏命令开发ASP组件
·VBScript教程 第二课 在HTML页面中添加VBscript代码
·VBScript教程 第一课 什么是 VBScript
如何使用FSO搜索硬盘文件
日期:2005年4月28日 作者:热点网络学院 查看:[大字体 中字体 小字体]

<%@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.knowsky.com</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>


 

(出处:http://www.vipcn.com

关于我们 | 帮助(?) | 版权声明 | 友情连接 
Copyright 2005-2005 viphot.com All Rights Reserved.
Powered by:mesky