VBAでファイルを検索する
2023/06/12
- code
Sub searchFiles() Dim myFolder As String, fileCondition As String Call setVariables myFolder = getSettingValue("Folder") fileCondition = getSettingValue("FileCondition") filesSheet.Activate filesSheet.Cells.ClearContents filesSheet.Range("B1").Value = "Folder" filesSheet.Range("C1").Value = "Filename" Call searchFilesEach(myFolder, fileCondition) End Sub Private Sub searchFilesEach(myFolder As String, fileCondition As String) Dim rowIndex As Long, buf As String, mySubFolder As Object buf = Dir(myFolder & "\*.*") Do While buf <> "" If fileCondition = "" Or buf Like fileCondition Then rowIndex = filesSheet.Range("B" & Rows.Count).End(xlUp).Row + 1 filesSheet.Range("B" & rowIndex).Value = myFolder filesSheet.Range("C" & rowIndex).Value = buf End If buf = Dir() Loop ' search in sub folder With CreateObject("Scripting.FileSystemObject") For Each mySubFolder In .GetFolder(myFolder).SubFolders Call searchFilesEach(mySubFolder.Path, fileCondition) Next End With End Sub