looping through files in folder

vbacoder

Active Member
Joined
Jul 7, 2007
Messages
354
Hi everyone,

I'm trying to write code to loop through a list of files in a folder. I would like to 'collect' the file names into an array and print them out to a text file.

I would be grateful if anyone could give any pointers on doing this. I've looked online for sample code, but the examples are a bit complex and difficult to follow.

Thanks,

vcoder
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Does this example help?
Code:
Sub SampleLoopFiles(strParentFolder As String)
    Dim FS As FileSearch
    Dim lngCounter As Long
    Set FS = Application.FileSearch
    With FS
        .NewSearch
        .LookIn = strParentFolder
        .SearchSubFolders = True
        .FileName = "*.xls"
        .MatchTextExactly = True
        .FileType = msoFileTypeExcelWorkbooks
        .Execute
        ' Loop through all the found files
        For lngCounter = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(lngCounter)
        Next lngCounter
    End With
    Set FS = Nothing
End Sub
 
Upvote 0

vbacoder

Active Member
Joined
Jul 7, 2007
Messages
354
Thanks for your reply Rorya,

This looks helpful. I would also like to place the contents of the folder into an array, so that I can perform functions or read the list into something else at another stage.

Thanks,

vcoder
 
Upvote 0

Forum statistics

Threads
1,190,658
Messages
5,982,138
Members
439,759
Latest member
Akashcm

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
Top