Help looping through files in a directory

statfreak

New Member
Joined
May 3, 2011
Messages
5
I have the code below. It runs, but only counts the first file and then ends. There are 12 files in the directory that should have been written to the Files Processed worksheet and counted.

My understanding is the 'fil = Dir' line should advance to the next file, but it is just returning an empty string ("") instead of the next file.

Any suggestions and help appreciated.

Code:
Option Explicit

Sub countfiles()

    Dim z As Long, totfiles As Long
    Dim WS As Worksheet
    Dim fLdr As String, Fil As String, myCurrFile As String, workshtName As String
    
    'define variables
    myCurrFile = ThisWorkbook.name
    z = 0
    totfiles = 0
    
    Workbooks(myCurrFile).Activate
    
    'turn off screen updating while macro is running
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
    End With
        
    Set WS = Sheets("Files Processed")

    fLdr = ActiveWorkbook.Path
    
    If Right(fLdr, 1) <> "\" Then fLdr = fLdr & "\"
    
    ChDir fLdr
    Fil = Dir("*.csv")
    
    Do While Fil <> ""
        z = z + 1
        WS.Cells(z + 1, 1).Resize(, 4) = _
            Array(Dir(Fil), _
            FileLen(Fil) / 1000, _
            FileDateTime(Fil), _
            fLdr)
        totfiles = totfiles + 1
        Fil = Dir
    Loop
    
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
        .StatusBar = False
    End With
    
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I tried that and it gets caught in an endless loop just repeating the first file over and over again.

Appreciate the reply though.
 
Upvote 0
Sorry, I wasn't clear where I meant

Code:
 Fil = Dir(fLdr & "*.csv")
    
    Do While Fil <> ""
        Z = Z + 1
        WS.Cells(Z + 1, 1).Resize(, 4) = _
            Array(Dir(Fil), _
            FileLen(Fil) / 1000, _
            FileDateTime(Fil), _
            fLdr)
        totfiles = totfiles + 1
        Fil = Dir
    Loop
 
Upvote 0
Ah, I see. Tried using
Code:
Fil = Dir(fLdr & "*.csv")
instead of
Code:
Fil = Dir("*.csv")
and received the same result, only the first file is counted & written.
 
Upvote 0
Sorry, I can't see why it is doing that. This is a similar piece of code that definitely works

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
Sorry, I can't see why it is doing that. This is a similar piece of code that definitely works

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
Yes, it does. :)

Thank you for the help.
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,310
Members
452,906
Latest member
phanmemchatdakenhupviral

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