SKY外语计算机学习

标题: 使用递归 搜索文件 [打印本页]

作者: SKY定格    时间: 2012-5-14 20:13
标题: 使用递归 搜索文件
本帖最后由 sky_yx 于 2015-12-30 14:22 编辑
  1. 加一个list
  2. Function SearchFiles(Path As String, FileType As String)
  3. Dim Files() As String '文件路径
  4. Dim Folder() As String '文件夹路径
  5. Dim a, b, c As Long
  6. Dim sPath As String
  7. sPath = Dir(Path & FileType) '查找第一个文件
  8. Do While Len(sPath) '循环到没有文件为止
  9. a = a + 1
  10. ReDim Preserve Files(1 To a)
  11. Files(a) = Path & sPath '将文件目录和文件名组合,并存放到数组中
  12. List1.AddItem Files(a) '加入list控件中
  13. sPath = Dir '查找下一个文件
  14. DoEvents '让出控制权
  15. Loop
  16. sPath = Dir(Path & "\", vbDirectory) '查找第一个文件夹
  17. Do While Len(sPath) '循环到没有文件夹为止
  18. If Left(sPath, 1) <> "." Then '为了防止重复查找
  19. If GetAttr(Path & "\" & sPath) And vbDirectory Then '如果是文件夹则。。。。。。
  20. b = b + 1
  21. ReDim Preserve Folder(1 To b)
  22. Folder(b) = Path & sPath & "\" '将目录和文件夹名称组合形成新的目录,并存放到数组中
  23. End If
  24. End If
  25. sPath = Dir '查找下一个文件夹
  26. DoEvents '让出控制权
  27. Loop
  28. For c = 1 To b '使用递归方法,遍历所有目录
  29. SearchFiles Folder(c), FileType
  30. Next
  31. End Function
  32. Private Sub Command1_Click() '调用
  33. SearchFiles "c:\", "*.txt"
  34. End Sub
复制代码







欢迎光临 SKY外语计算机学习 (http://www.skywj.com/) Powered by Discuz! X2.5