[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[已解决]VBS全盘搜索目标文件

我知道在批处理中可以利用for和dir /s的组合来进行全盘的文件搜索,我想问在VBS中这是如何实现同样的功能,或者说有什么方法和函数能实现这个功能? 打个比方,我想在D盘中搜索一个叫"XXXXX.ttt"的文件,D盘有成百上千个目录及子目录,这该如何做这个脚本?

[ 本帖最后由 s700800900 于 2010-7-26 11:31 编辑 ]
1

评分人数

    • Batcher: 感谢主动给标题标注[已解决]字样PB + 2

楼主,提问前可以google baidu一下,有些东西是现成的,转载一个
  1. on error resume next
  2. Dim keyWord, DirTotal, TimeSpend, FileTotal, Fso, outFile, txtResult, txtPath, sPath
  3. Const MY_COMPUTER = &H11&
  4. Const WINDOW_HANDLE = 0
  5. Const OPTIONS = 0
  6. Set objShell = CreateObject("Shell.Application")
  7. Set objFolder = objShell.Namespace(My_Computer)
  8. Set objFolderItem = objFolder.Self
  9. strPath = objFolderItem.Path
  10. Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "选择你要搜索的文件夹:", OPTIONS, strPath)
  11. If objFolder Is Nothing Then
  12. msgbox "您没有选择任何有效目录!"
  13. wscript.quit
  14. else
  15. Set objFolderItem = objFolder.Self
  16. sPath = objFolderItem.Path
  17. txtpath=sPath
  18. Set Fso = wscript.CreateObject("scripting.filesystemobject")
  19. FileTotal = 0
  20. DirTotal = 0
  21. 'sPath = left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))
  22. 'txtPath = trim(inputbox("你选的目录是"&sPath,"文件搜索",sPath))
  23. keyWord = LCase(inputbox("请输入搜索关键字点Cancel的话会得到目录列表:","文件搜索","mp3"))
  24. set outFile = Fso.createtextfile(sPath & "\SearchResult.txt")
  25. outFile.writeline "开始搜索..."
  26. outFile.writeline "起启目录:" & txtPath
  27. TimeSpend = Timer
  28. myFind txtPath
  29. TimeSpend = round(Timer - TimeSpend,2)
  30. txtResult = "搜索完成!" & vbCrLf & "共找到文件:" & FileTotal & "个." & vbCrLf & "共搜索目录:" & DirTotal & "个." & vbCrLf & "用时:" & TimeSpend & "秒."
  31. outFile.write txtResult
  32. msgbox txtResult &"结果保存在"&sPath &"\SearchResult.txt"
  33. outFile.close
  34. set outFile = nothing
  35. set Fso = nothing
  36. Sub myFind(ByVal thePath)
  37. Dim fso, myFolder, myFile, curFolder
  38. Set fso = wscript.CreateObject("scripting.filesystemobject")
  39. Set curFolders = fso.getfolder(thePath)
  40. DirTotal = DirTotal + 1
  41. If curFolders.Files.Count > 0 Then
  42. For Each myFile In curFolders.Files
  43. If InStr(1, LCase(myFile.Name), keyWord) > 0 Then
  44. outFile.WriteLine FormatPath(thePath) & "\" & myFile.Name
  45. FileTotal = FileTotal + 1
  46. End If
  47. Next
  48. End If
  49. If curFolders.subfolders.Count > 0 Then
  50. For Each myFolder In curFolders.subfolders
  51. myFind FormatPath(thePath) & "\" & myFolder.Name
  52. Next
  53. End If
  54. End Sub
  55. Function FormatPath(ByVal thePath)
  56. thePath = Trim(thePath)
  57. FormatPath = thePath
  58. If Right(thePath, 1) = "\" Then FormatPath = Mid(thePath, 1, Len(thePath) - 1)
  59. End Function
  60. End if
复制代码


TOP

额,不知道是不是我关键字的问题,我找到的都是只搜索一二级目录的脚本,这个脚本我去研究研究,非常感谢。

TOP

大概了解了它的运行原理,不断地调用函数对目录循环检测=。=!

TOP

返回列表