乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 74|回复: 4

具有子目录的最佳查找文件

[复制链接]
382

1

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
5
发表于 2009-1-2 16:08:41 | 显示全部楼层 |阅读模式
我一直在四处寻找,找不到搜索包含子目录的文件的方法 Excel有一个.FileSearch方法,但它没有#039;有什么想法吗?
回复

使用道具 举报

0

主题

13

帖子

6

银币

初来乍到

Rank: 1

铜币
13
发表于 2009-1-2 16:19:09 | 显示全部楼层
不检查每个目录中的其他目录,然后检查它们,I'我不确定。
回复

使用道具 举报

0

主题

8

帖子

4

银币

初来乍到

Rank: 1

铜币
8
发表于 2009-1-2 16:33:58 | 显示全部楼层
最好的当然不是,这是我大约一百年前写的(don #039;t笑)。你认为合适的虐待
  1. Option Explicit
  2. [color=red]Public[/color] Sub Test ( )
  3.     Dim foldername As String, _
  4.         filespec   As String, _
  5.         var        As Variant
  6.         
  7.     foldername = "c:\windows\temp"
  8.    
  9. [color=green]    ''  you could use this to reduce the initial scan results[/color]
  10.    
  11.     filespec = "*.*"
  12.    
  13. [color=green]    ''  in a real world app you'd want to set the result to a collection
  14.     ''  and then test the count before attempting to iterate it[/color]
  15.    
  16.     For Each var In GetAllFiles(foldername, filespec, True)
  17.         
  18.         Debug.Print var
  19.         
  20. [color=green]        ''  do your final file matching code here if filespec doesn't nail it[/color]
  21.         
  22.     Next var
  23. End Sub
  24. [color=blue]Public[/color] Function GetAllFiles(foldername As String, filespec As String, recurse As Boolean) As Collection
  25.     Dim result As New Collection
  26.         
  27.     Call GetAllFilesEx(foldername, filespec, recurse, result)
  28.    
  29.     Set GetAllFiles = result
  30. End Function
  31. [color=red]Private[/color] Sub GetAllFilesEx(foldername As String, _
  32.                           filespec As String, _
  33.                           recurse As Boolean, _
  34.                           ByRef result As Collection)
  35.     DoEvents
  36.     Dim filename As String, _
  37.         fullname As String, _
  38.         folder   As Variant, _
  39.         folders  As New Collection        
  40.         
  41.     If recurse Then
  42.    
  43. [color=green]        ''  get any folders[/color]
  44.         
  45.         filename = Dir$(foldername & "\*.*", vbDirectory)
  46.         
  47.         Do While filename  ""
  48.             If filename = "." Then GoTo Iterate
  49.             If filename = ".." Then GoTo Iterate
  50.             fullname    = foldername & "" & filename
  51.             If GetAttr(fullname) And vbDirectory Then
  52.                 folders.Add fullname
  53.             End If
  54. Iterate:
  55.             filename = Dir$
  56.         Loop
  57.         
  58.     End If
  59.    
  60. [color=green]    ''  get the files[/color]
  61.    
  62.     filename = Dir$(foldername & "" & filespec, vbHidden)
  63.    
  64.     Do While filename  ""
  65.         fullname = foldername & "" & filename
  66.         result.Add fullname
  67.         filename = Dir$
  68.     Loop
  69.    
  70.     For Each folder In folders
  71.         Call GetAllFilesEx(CStr(folder), filespec, recurse, result)
  72.     Next folder
  73. End Sub

编辑:修复了拼写错误。
回复

使用道具 举报

0

主题

9

帖子

5

银币

初来乍到

Rank: 1

铜币
9
发表于 2009-1-2 16:59:14 | 显示全部楼层
谢谢,我看看我能不能做到
回复

使用道具 举报

0

主题

9

帖子

5

银币

初来乍到

Rank: 1

铜币
12
发表于 2009-1-3 11:50:31 | 显示全部楼层

我不时发布以下模块。它使用api调用,因此速度稍快,只允许搜索某些FileNaName扩展,并在模块末尾包含一个子例程,每次找到指定文件时都会调用该子例程,因此您可以执行诸如打开*之类的操作。dwg,打印a*。txt等
  1. Option Explicit
  2. Private Const vbDot = 46
  3. Private Const MAX_PATH As Long = 260
  4. Private Const INVALID_HANDLE_VALUE = -1
  5. Private Const vbBackslash = ""
  6. Private Const ALL_FILES = "*.*"
  7. Private Type FILETIME
  8.     dwLowDateTime As Long
  9.     dwHighDateTime As Long
  10. End Type
  11. Private Type WIN32_FIND_DATA
  12.     dwFileAttributes As Long
  13.     ftCreationTime As FILETIME
  14.     ftLastAccessTime As FILETIME
  15.     ftLastWriteTime As FILETIME
  16.     nFileSizeHigh As Long
  17.     nFileSizeLow As Long
  18.     dwReserved0 As Long
  19.     dwReserved1 As Long
  20.     cFileName As String * MAX_PATH
  21.     cAlternate As String * 14
  22. End Type
  23. Private Declare Function FindClose Lib "kernel32" _
  24.     (ByVal hFindFile As Long) As Long
  25. Private Declare Function FindFirstFile Lib "kernel32" _
  26.     Alias "FindFirstFileA" (ByVal lpFileName As String, _
  27.     lpFindFileData As WIN32_FIND_DATA) As Long
  28. Private Declare Function FindNextFile Lib "kernel32" _
  29.     Alias "FindNextFileA" (ByVal hFindFile As Long, _
  30.     lpFindFileData As WIN32_FIND_DATA) As Long
  31. Private Declare Function lstrlen Lib "kernel32" _
  32.     Alias "lstrlenW" (ByVal lpString As Long) As Long
  33. Private Declare Function PathMatchSpec Lib "shlwapi" _
  34.     Alias "PathMatchSpecW" (ByVal pszFileParam As Long, _
  35.     ByVal pszSpec As Long) As Long
  36. Dim sFileExt As String
  37. Dim sFileRoot As String
  38. Public Sub FindAllFiles(FileType As String, StartPath As String)
  39.     ' recursively searches a passed path for files of a given type
  40.     ' FileType: a wildcard string of the file extension, ie; "*.dwg" or "*.xls"
  41.     ' calls "DoSomethingWithFile" subroutine to execute your own code on each found file
  42.    
  43.     sFileRoot = QualifyPath(StartPath) 'start path
  44.     sFileExt = FileType 'file type of interest
  45.    
  46.     Call SearchForFiles(sFileRoot)
  47. End Sub
  48. Private Sub SearchForFiles(sRoot As String)
  49.     Dim WFD As WIN32_FIND_DATA
  50.     Dim hFile As Long
  51.    
  52.     hFile = FindFirstFile(sRoot & ALL_FILES, WFD)
  53.    
  54.     If hFile  INVALID_HANDLE_VALUE Then
  55.         
  56.         Do
  57.         'if a folder, and recurse specified, call method again
  58.         If (WFD.dwFileAttributes And vbDirectory) Then
  59.             If Asc(WFD.cFileName)  vbDot Then
  60.                 SearchForFiles sRoot & TrimNull(WFD.cFileName) & vbBackslash
  61.             End If
  62.         Else
  63.             'must be a file..
  64.             If MatchSpec(WFD.cFileName, sFileExt) Then
  65.                 DoSomethingWithFile sRoot & TrimNull(WFD.cFileName)
  66.             End If 'If MatchSpec
  67.         End If 'If WFD.dwFileAttributes
  68.         
  69.         Loop While FindNextFile(hFile, WFD)
  70.     End If 'If hFile
  71.    
  72.     Call FindClose(hFile)
  73. End Sub
  74. Private Function QualifyPath(sPath As String) As String
  75.     ' formats passed path string to be used in recursive API search
  76.     If Right$(sPath, 1)  vbBackslash Then
  77.         QualifyPath = sPath & vbBackslash
  78.     Else
  79.         QualifyPath = sPath
  80.     End If
  81. End Function
  82. Private Function TrimNull(startstr As String) As String
  83.     ' trims NULL char (ascii 0) from strings returned by API calls
  84.     TrimNull = Left$(startstr, lstrlen(StrPtr(startstr)))
  85. End Function
  86. Private Function MatchSpec(sFile As String, sSpec As String) As Boolean
  87.     ' uses API version of the "LIKE" command
  88.     MatchSpec = PathMatchSpec(StrPtr(sFile), StrPtr(sSpec))
  89. End Function
  90. Private Sub DoSomethingWithFile(FoundFileName As String)
  91.     ' use this routine to do something with each file found
  92.     ' we'll do nothing but print out the filename found
  93.     Dim FoundName As String: FoundName = FoundFileName
  94.    
  95.     Debug.Print FoundName
  96. End Sub
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-4 20:08 , Processed in 0.463454 second(s), 63 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表