Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Directory content from two different folders with Excel VBA

Posted on 2014-07-23
5
Medium Priority
?
360 Views
Last Modified: 2014-07-24
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(mySourcePath)
    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.Path, True)
        Next
    End If
End Sub
0
Comment
Question by:kieranjcollins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40216108
Use this code, it assumes that the path is in A1:A3 and B1:B3 is either true or false depending on whether subfolders should be included.
Sub ListFiles()
    Dim rng As Range, c As Range
    Set rng = Range("A1:A3")
    For Each c In rng.Cells
        Call ListMyFiles(Range("A" & c.Row), Range("B" & c.Row))
    Next
End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders)
    Dim iRow As Long, iCol As Long
    Dim MyObject As Object, mySource As Object, myFile As Object, mySubFolder As Object
    iRow = 11
    Set MyObject = CreateObject("Scripting.FileSystemObject")
    Set mySource = MyObject.GetFolder(mySourcePath)
    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.Path, True)
        Next
    End If
End Sub

Open in new window

0
 

Author Comment

by:kieranjcollins
ID: 40216204
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?
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 40216266
Try this:
Dim iRow As Long

Sub ListFiles()
    Dim rng As Range, c As Range
    Set rng = Range("A1:A3")
    iRow = IIf(iRow = 1048576, Range("C1").End(xlDown).Row, 1)
    For Each c In rng.Cells
        Call ListMyFiles(Range("A" & c.Row), Range("B" & c.Row))
    Next
End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders)
    Dim MyObject As Object, mySource As Object, myFile As Object, mySubFolder As Object
    Set MyObject = CreateObject("Scripting.FileSystemObject")
    Set mySource = MyObject.GetFolder(mySourcePath)
    On Error Resume Next
    For Each myFile In mySource.Files
        Cells(iRow, 3).Value = myFile.Path
        Cells(iRow, 4).Value = myFile.Name
        Cells(iRow, 5).Value = myFile.Size
        Cells(iRow, 6).Value = myFile.DateLastModified
        iRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row + 1
    Next
    If IncludeSubfolders Then
        For Each mySubFolder In mySource.SubFolders
            Call ListMyFiles(mySubFolder.Path, True)
        Next
    End If
    ActiveSheet.UsedRange.Columns.AutoFit
End Sub

Open in new window

0
 

Author Closing Comment

by:kieranjcollins
ID: 40216407
That worked a treat, thank you very much!
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40216427
You're welcome.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question