kieranjcollins
asked on
Directory content from two different folders with Excel VBA
Hi,
I'm looking to use VBA to retrieve the contents of two or three different folders and combine results.
e.g.
C:\Users\User\Music
C:\Users\User\Videos
I have used the below, but it has to be run twice
Dim iRow
Sub ListFiles()
iRow = 11
Call ListMyFiles(Range("C7"), Range("C8"))
End Sub
Sub ListMyFiles(mySourcePath, IncludeSubfolders)
Set MyObject = New Scripting.FileSystemObject
Set mySource = MyObject.GetFolder(mySourc ePath)
On Error Resume Next
For Each myFile In mySource.Files
iCol = 2
Cells(iRow, iCol).Value = myFile.Path
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Name
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Size
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.DateLastModified
iRow = iRow + 1
Next
If IncludeSubfolders Then
For Each mySubFolder In mySource.SubFolders
Call ListMyFiles(mySubFolder.Pa th, True)
Next
End If
End Sub
I'm looking to use VBA to retrieve the contents of two or three different folders and combine results.
e.g.
C:\Users\User\Music
C:\Users\User\Videos
I have used the below, but it has to be run twice
Dim iRow
Sub ListFiles()
iRow = 11
Call ListMyFiles(Range("C7"), Range("C8"))
End Sub
Sub ListMyFiles(mySourcePath, IncludeSubfolders)
Set MyObject = New Scripting.FileSystemObject
Set mySource = MyObject.GetFolder(mySourc
On Error Resume Next
For Each myFile In mySource.Files
iCol = 2
Cells(iRow, iCol).Value = myFile.Path
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Name
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Size
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.DateLastModified
iRow = iRow + 1
Next
If IncludeSubfolders Then
For Each mySubFolder In mySource.SubFolders
Call ListMyFiles(mySubFolder.Pa
Next
End If
End Sub
ASKER
The listing of the second folder contents in the above start populating on the first row, overwriting the contents of the first folder.
Can the code work so that folder 2 contents are stacked on folder 1 contents etc?
Can the code work so that folder 2 contents are stacked on folder 1 contents etc?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked a treat, thank you very much!
You're welcome.
Open in new window