List the paths of all files in a folder and subfolder

Status
Not open for further replies.

Joe9238

Board Regular
Joined
Jul 14, 2017
Messages
67
Hi, I need a code that will paste a list of file paths contained within a folder and all its sub folders and all the sub folders withing those sub folders and so on. I have currently got a code that will ask you to pick a file and will then list the file paths in that folder whilst ignoring sub folders. Thanks in advance :)

Code:
Sub GatherData()Dim FileSystem As Object
Dim HostFolder As String


    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder..."
        .Show
        If .SelectedItems.Count > 0 Then
            HostFolder = .SelectedItems(1) & "\"
            'sFoldersubfolder =
        Else
            Exit Sub
        End If
    End With


Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
End Sub
Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.Subfolders
        DoFolder SubFolder
    Next
    Dim File
    
    Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer


Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objFolder = objFSO.GetFolder("C:\Users\joema\Documents\Data Miner\Sub folder")
i = 1
    
    For Each File In Folder.Files
        ' Operate on each file


Cells(i + 1, 15) = File.Name
Cells(i + 1, 16) = File.Path
i = i + 1


    Next
End Sub
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Status
Not open for further replies.

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top