VBA loop through directory, store names

BigLRIP

Board Regular
Joined
May 7, 2009
Messages
123
Hi all,

I'm looking for a VBA function that will loop through a directory, and store every *.pub file into an array. I want each value in the array to be the full path to the *.pub file, not just the name.

In Office 2003 I used to do the following, but .FileSearch does not work in office 2007:
Code:
Sub GrabTextFromDashboards(DBPath As String, TXTPath As String)
    
    Application.ScreenUpdating = False
    
    Dim intCount As Integer
    Dim arrFiles() As String
    Dim arrFileNames() As String
    Dim strPath, strName, strFilePath As String
    Dim strWriteup As String
    Dim lngPathLen As Long
 
    strPath = DBPath
    lngPathLen = Len(strPath)

    With Application.FileSearch
           .NewSearch
           .FileName = "*.pub"
           .LookIn = strPath
           .Execute
        
           For intCount = 1 To .FoundFiles.Count
           Next intCount
        
           ReDim arrFiles(1 To intCount) As String
           ReDim arrFileNames(1 To intCount) As String
        
           '********** Stores the path of all files into an Array
           For intCount = 1 To .FoundFiles.Count
               arrFiles(intCount) = .FoundFiles(intCount)
           Next intCount
           .
           .
           .
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You should be able to use DIR. Example

Code:
Sub ListFiles()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer
MyFolder = "C:\example"
MyFile = Dir(MyFolder & "\*.xls")
Do While MyFile <> ""
    j = j + 1
    Cells(j, 1).Value = MyFile
    MyFile = Dir
Loop
End Sub
 
Upvote 0
Thanks VoG!

now I have

Code:
Dim MyFile As String
    Dim j As Integer
    j = 0
    MyFile = Dir(strPath & "\*.pub")
    Do While MyFile <> ""
        j = j + 1
        MyFile = Dir
    Loop
    
    ReDim arrFiles(1 To j) As String
    ReDim arrFileNames(1 To j) As String
    
    Dim k As Integer
    k = 1
    MyFile = Dir(strPath & "\*.pub")
    Do While MyFile <> ""
        arrFiles(k) = strPath & "\" & MyFile
        k = k + 1
        MyFile = Dir
    Loop

Any idea why my array fails to store the last file in the directory?
 
Upvote 0
You don't need to loop through the directory twice, e.g:
Code:
Dim arrFiles
Dim MyFile As String
Dim j As Long
j = 0

MyFile = Dir(strpath & "\*.pub")

Do While Len(MyFile) > 0
    MyFile = Dir
    ReDim Preserve arrFiles(j)
    arrFiles(j) = strpath & "\" & MyFile
    j = j + 1
Loop
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,317
Messages
6,171,425
Members
452,402
Latest member
siduslevis

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