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: