标题: [已解决]VBS全盘搜索目标文件 [打印本页]
作者: s700800900 时间: 2010-7-25 22:54 标题: [已解决]VBS全盘搜索目标文件
我知道在批处理中可以利用for和dir /s的组合来进行全盘的文件搜索,我想问在VBS中这是如何实现同样的功能,或者说有什么方法和函数能实现这个功能? 打个比方,我想在D盘中搜索一个叫"XXXXX.ttt"的文件,D盘有成百上千个目录及子目录,这该如何做这个脚本?
[ 本帖最后由 s700800900 于 2010-7-26 11:31 编辑 ]
作者: qaz123654 时间: 2010-7-26 10:05
楼主,提问前可以google baidu一下,有些东西是现成的,转载一个- on error resume next
- Dim keyWord, DirTotal, TimeSpend, FileTotal, Fso, outFile, txtResult, txtPath, sPath
- Const MY_COMPUTER = &H11&
- Const WINDOW_HANDLE = 0
- Const OPTIONS = 0
- Set objShell = CreateObject("Shell.Application")
- Set objFolder = objShell.Namespace(My_Computer)
- Set objFolderItem = objFolder.Self
- strPath = objFolderItem.Path
- Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "选择你要搜索的文件夹:", OPTIONS, strPath)
- If objFolder Is Nothing Then
- msgbox "您没有选择任何有效目录!"
- wscript.quit
- else
- Set objFolderItem = objFolder.Self
- sPath = objFolderItem.Path
- txtpath=sPath
- Set Fso = 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"))
- set outFile = Fso.createtextfile(sPath & "\SearchResult.txt")
- outFile.writeline "开始搜索..."
- outFile.writeline "起启目录:" & txtPath
- TimeSpend = Timer
- myFind txtPath
- TimeSpend = round(Timer - TimeSpend,2)
- txtResult = "搜索完成!" & vbCrLf & "共找到文件:" & FileTotal & "个." & vbCrLf & "共搜索目录:" & DirTotal & "个." & vbCrLf & "用时:" & TimeSpend & "秒."
- outFile.write txtResult
- msgbox txtResult &"结果保存在"&sPath &"\SearchResult.txt"
- outFile.close
- set outFile = nothing
- set Fso = nothing
- Sub myFind(ByVal thePath)
- Dim fso, myFolder, myFile, curFolder
- Set fso = wscript.CreateObject("scripting.filesystemobject")
- Set curFolders = fso.getfolder(thePath)
- DirTotal = DirTotal + 1
- If curFolders.Files.Count > 0 Then
- For Each myFile In curFolders.Files
- If InStr(1, LCase(myFile.Name), keyWord) > 0 Then
- outFile.WriteLine FormatPath(thePath) & "\" & myFile.Name
- FileTotal = FileTotal + 1
- End If
- Next
- End If
- If curFolders.subfolders.Count > 0 Then
- For Each myFolder In curFolders.subfolders
- myFind FormatPath(thePath) & "\" & myFolder.Name
- Next
- End If
- End Sub
- Function FormatPath(ByVal thePath)
- thePath = Trim(thePath)
- FormatPath = thePath
- If Right(thePath, 1) = "\" Then FormatPath = Mid(thePath, 1, Len(thePath) - 1)
- End Function
- End if
复制代码
作者: s700800900 时间: 2010-7-26 11:09
额,不知道是不是我关键字的问题,我找到的都是只搜索一二级目录的脚本,这个脚本我去研究研究,非常感谢。
作者: s700800900 时间: 2010-7-26 11:31
大概了解了它的运行原理,不断地调用函数对目录循环检测=。=!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |