title: In what order does the Dir() function select the next relevant file in a "Get Names of All Files" loop?
body: <p>I have part of code which goes over the files of a folder and assigns a number to them. These files are zip files generated with a &quot;run number-date-time.7z&quot; name format. Their numbers and date/time stamps are in order. It is important for my application that the files are read by the Dir()-loop in the correct order also but that does not happen (see attached image).</p>
  <p>Can someone help me understand why this is and how to correct it?</p>
  <p>Any help is much appreciated!</p>
  <pre class="lang-vb prettyprint-override"><code>Sub Test()
  
      Dim sPath As String, archiveFiles() As String
      Dim wbTarget As Workbook
      Dim rSrc As Range
      Dim rDst As Range
      
      targetWorkbook = ThisWorkbook.Name
      sPath = ThisWorkbook.Path
      
      folderName = &quot;D:\New folder\Archives&quot;
      
     
      'Look in folder for archive files
      If Right(folderName, 1) &lt;&gt; &quot;\&quot; Then
          folderName = folderName &amp; &quot;\&quot;
      End If
      
      j = 0
      archiveFile = Dir(folderName &amp; &quot;*.7z&quot;)
      Do Until archiveFile = vbNullString
          ReDim Preserve archiveFiles(j + 1)
          archiveFiles(j) = folderName &amp; &quot;\&quot; &amp; archiveFile
          archiveFile = Dir()
          j = j + 1
      Loop
  
  
  End Sub
  </code></pre>
  <p><a href="https://i.stack.imgur.com/8QD9Q.png" rel="nofollow noreferrer">Locals result</a></p>