0
ディレクトリを検索するコードがオンライン上にあり、検索条件を満たすファイルのサブディレクトリです。最初に一致するファイルが Excel VBA:いくつかのサブディレクトリを除いたファイルのフォルダとサブディレクトリを検索
- 停止:私はこのコードを編集したい
など)
フォルダーの例は、「ツールの履歴」を含める無視するように、ディレクトリ構造を作成した者は、ファイル名に「ツール史」
内のすべてのサブディレクトリをスペースを使用しています210私が発見したコードは、このコードは非常に遅いので、誰もが速く何かを持っている場合、私は本当に感謝される
Function RecursiveDir(colFiles As Collection, _
strFolder As String, _
strFileSpec As String, _
bIncludeSubfolders As Boolean)
' Search a folder and each of its subfolders for any files that meet the citerion given in
' strFileSpec
' colFiles - the name of the collection to add the output to
' strFolder - The path to the parent directory
' strFileSpec - The condition of the filename being searched for (for example all pdf files)
' bIncludeSubfolders - Boolean, include all subfolders in the search
' THIS FUNCTION IS SUBOPTIMAL AND VERY SLOW, PLEASE REVISIT IF USED REGULARLY
Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant
'Add files in strFolder matching strFileSpec to colFiles
strFolder = TrailingSlash(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colFiles.Add strFolder & strTemp
strTemp = Dir
Loop
If bIncludeSubfolders Then
'Fill colFolders with list of subdirectories of strFolder
strTemp = Dir(strFolder, vbDirectory)
Do While strTemp <> vbNullString
If (strTemp <> ".") And (strTemp <> "..") Then
If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
colFolders.Add strTemp
End If
End If
strTemp = Dir
Loop
'Call RecursiveDir for each subfolder in colFolders
For Each vFolderName In colFolders
Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
Next vFolderName
End If
End Function
Function TrailingSlash(strFolder As String) As String
' Search for and remove a trailing slash in the directory pathname
If Len(strFolder) > 0 Then
If Right(strFolder, 1) = "\" Then
TrailingSlash = strFolder
Else
TrailingSlash = strFolder & "\"
End If
End If
End Function
(私はそれを見つけた場所のソースを参照していないため申し訳ありませんが、私は覚えていないことができます)以下であります。
感謝
ありがとうございます。ありがとうございます。名前に「履歴」がないすべてのサブディレクトリのすべてのファイルがリストされています。すばらしいです!ここで、このコードを更新して名前が "* test.pdf"のファイルのみをリストしたいとしたら、どうすればいいでしょうか? – jlt199