1

I am trying to loop through all subfolders within a folder, compiling all the names of the files. I'm having a bit of trouble though as I have adapted this code from a macro that only loops through one folder and compiles file names. Thank you greatly for your time!

Sub FindFiles()

Cells(1, 1).Select

Dim F As String
Dim G As String

F = Dir("C:\Users\z003nttv\Desktop\Folder\" & "*")
Do While Len(F) > 0

Do While Len(G) > 0
G = Dir(F & "*.*")
ActiveCell.Formula = G
ActiveCell.Offset(1, 0).Select
G = Dir()
Loop
F = Dir()
Loop

End Sub

2 Answers 2

1

Found the answer at the following site:

https://www.extendoffice.com/documents/excel/2994-excel-list-all-files-in-folder-and-subfolders.html

Hope it may lend some help..very user friendly!

Sign up to request clarification or add additional context in comments.

Comments

1

I was working on this code before you posted the link and had second thoughts posting my work. So I checked out the link you posted. I believe that the code I wanted to post is succinct & addresses the point far better. Hence here you go

Step 1. The Reference

References needed for this code

Step2. The Code.

Sub FindFiles()
Dim fso As FileSystemObject
Dim Folder As Folder
Dim Files As Files
Dim path, s As String

'' Input Path : Please change as needed
path = Sheets("Sheet1").Cells(1, 2).Value

Set fso = New FileSystemObject
Set Basefolder = fso.GetFolder(path)
Set SubFolders = Basefolder.SubFolders
Dim i As Integer
i = 1
For Each Folder In SubFolders
    Set Files = Folder.Files
    For Each File In Files
       With Sheets("Sheet1")
       .Cells(i, 1).Value = Folder
       .Cells(i, 2).Value = File
       i = i + 1
    Next File
Next Folder
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.