快速业务通道

vbs搜索文件名或者得到目录列表

作者 佚名 来源 ASP编程 浏览 发布时间 2013-07-09
''把网上的一个小程序改得方便了点,这个搜索次效率很好。
复制代码 代码如下:

onerrorresumenext
DimkeyWord,DirTotal,TimeSpend,FileTotal,Fso,outFile,txtResult,txtPath,sPath
ConstMY_COMPUTER=&H11&
ConstWINDOW_HANDLE=0
ConstOPTIONS=0
SetobjShell=CreateObject("Shell.Application")
SetobjFolder=objShell.Namespace(My_Computer)
SetobjFolderItem=objFolder.Self
strPath=objFolderItem.Path

SetobjFolder=objShell.BrowseForFolder(WINDOW_HANDLE,"选择你要搜索的文件夹:",OPTIONS,strPath)
IfobjFolderIsNothingThen
msgbox"您没有选择任何有效目录!"
wscript.quit
else
SetobjFolderItem=objFolder.Self
sPath=objFolderItem.Path
txtpath=sPath
SetFso=wscript.CreateObject("scripting.filesystemobject")

FileTotal=0
DirTotal=0

''sPath=left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))
''txtPath=trim(inputbox("你选的目录是"&sPath,"文件搜索",sPath))

keyWord=LCase(inputbox("请输入搜索关键字点Cancel的话会得到目录列表:","文件搜索","mp3"))

setoutFile=Fso.createtextfile(sPath&"\SearchResult.txt")

outFile.writeline"开始搜索..."
outFile.writeline"起启目录:"&txtPath
TimeSpend=Timer

myFindtxtPath

TimeSpend=round(Timer-TimeSpend,2)

txtResult="搜索完成!"&vbCrLf&"共找到文件:"&FileTotal&"个."&vbCrLf&"共搜索目录:"&DirTotal&"个."&vbCrLf&"用时:"&TimeSpend&"秒."
outFile.writetxtResult
msgboxtxtResult&"结果保存在"&sPath&"\SearchResult.txt"

outFile.close
setoutFile=nothing
setFso=nothing

SubmyFind(ByValthePath)

Dimfso,myFolder,myFile,curFolder
Setfso=wscript.CreateObject("scripting.filesystemobject")
SetcurFolders=fso.getfolder(thePath)
DirTotal=DirTotal+1
IfcurFolders.Files.Count>0Then
ForEachmyFileIncurFolders.Files
IfInStr(1,LCase(myFile.Name),keyWord)>0Then
outFile.WriteLineFormatPath(thePath)&"\"&myFile.Name
FileTotal=FileTotal+1
EndIf
Next
EndIf

IfcurFolders.subfolders.Count>0Then
ForEachmyFolderIncurFolders.subfolders
myFindFormatPath(thePath)&"\"&myFolder.Name
Next
EndIf

EndSub

FunctionFormatPath(ByValthePath)

thePath=Trim(thePath)
FormatPath=thePath
IfRight(thePath,1)="\"ThenFormatPath=Mid(thePath,1,Len(thePath)-1)

EndFunction

Endif

=======================================================================

附件:关于打开目录的方面:

PrivateConstCSIDL_DESKTOP=&H0''<desktop>
PrivateConstCSIDL_INTERNET=&H1''InternetExplorer(iconondesktop)
PrivateConstCSIDL_PROGRAMS=&H2''StartMenu\Programs
PrivateConstCSIDL_CONTROLS=&H3''MyComputer\ControlPanel
PrivateConstCSIDL_PRINTERS=&H4''MyComputer\Printers
PrivateConstCSIDL_PERSONAL=&H5''MyDocuments
PrivateConstCSIDL_FAVORITES=&H6''<username>\Favorites
PrivateConstCSIDL_STARTUP=&H7''StartMenu\Programs\Startup
PrivateConstCSIDL_RECENT=&H8''<username>\Recent
PrivateConstCSIDL_SENDTO=&H9''<username>\SendTo
PrivateConstCSIDL_BITBUCKET=&HA''<desktop>\RecycleBin
PrivateConstCSIDL_STARTMENU=&HB''<username>\StartMenu
PrivateConstCSIDL_MYDOCUMENTS=&HC''logical"MyDocuments"desktopicon
PrivateConstCSIDL_MYMUSIC=&HD''"MyMusic"folder
PrivateConstCSIDL_MYVIDEO=&HE''"MyVideos"folder
PrivateConstCSIDL_DESKTOP

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号