Sub prcListFilesInFolderWithSubfolders()
Dim fso As FileSystemObject ' FileSystemObjectのインスタンス
Dim fld As Folder ' 処理中のフォルダ
Dim fldSub As Folder ' サブフォルダ
Dim fle As File ' 処理中のファイル
Dim rngOutput As Range ' 出力開始セル
Dim strFolder As String ' 処理するフォルダのパス
Dim lngRow As Long ' 出力開始行
lngRow = 1 ' 出力開始行の初期化
Set rngOutput = ThisWorkbook.Sheets("Sheet1").Cells(lngRow, 1)
Set fso = New FileSystemObject
strFolder = "フォルダAのパスをここに入力" ' 例: "C:\MyFolder"
Set fld = fso.GetFolder(strFolder)
Call prcRecurseFolders(fld, rngOutput, lngRow, fso)
Set fso = Nothing
MsgBox "処理が完了しました。", vbInformation
End Sub
Private Sub prcRecurseFolders(ByVal fld As Folder, ByRef rngOutput As Range, ByRef lngRow As Long, ByRef fso As FileSystemObject)
Dim fle As File
Dim fldSub As Folder
For Each fle In fld.Files
rngOutput.Worksheet.Cells(lngRow, 1).Value = fle.Path
lngRow = lngRow + 1
Next fle
For Each fldSub In fld.SubFolders
Call prcRecurseFolders(fldSub, rngOutput, lngRow, fso)
Next fldSub
End Sub
コメント