title: Loop through files in a folder using VBA?
body: <p>I would like to loop through the files of a directory using <a href="/questions/tagged/vba" class="post-tag" title="show questions tagged &#39;vba&#39;" rel="tag">vba</a> in Excel 2010. </p>
  
  <p>In the loop, I will need:</p>
  
  <ul>
  <li>the filename, and</li>
  <li>the date at which the file was formatted. </li>
  </ul>
  
  <p>I have coded the following which works fine if the folder has no more then 50 files, otherwise it is ridiculously slow (I need it to work with folders with >10000 files). The sole problem of this code is that the operation to look up <code>file.name</code> takes extremely much time. </p>
  
  <p><em>Code that works but is waaaaaay too slow (15 seconds per 100 files):</em></p>
  
  <pre><code>Sub LoopThroughFiles()
     Dim MyObj As Object, MySource As Object, file As Variant
     Set MySource = MyObj.GetFolder("c:\testfolder\")
     For Each file In MySource.Files
        If InStr(file.name, "test") &gt; 0 Then
           MsgBox "found"
           Exit Sub
        End If
     Next file
  End Sub
  </code></pre>
  
  <hr>
  
  <p><strong>Problem solved:</strong>                           </p>
  
  <ol>
  <li>My problem has been solved by the solution below using <code>Dir</code> in a particular way (20 seconds for 15000 files) and for checking the time stamp using the command <code>FileDateTime</code>.   </li>
  <li>Taking into account another answer from below the 20 seconds are reduced to less than 1 second. </li>
  </ol>