title: Loop Through All Subfolders - VBA - Queue method
body: <p>I've made use of Cor_blimey's queue method to write all the folders and subfolders of a drive to an excel sheet, as follows:</p>
  
  <pre><code>Public Sub NonRecursiveMethod()
      Dim fso, oFolder, oSubfolder, oFile, queue As Collection
  
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set queue = New Collection
      queue.Add fso.GetFolder("your folder path variable") 'obviously replace
  
      Do While queue.Count &gt; 0
          Set oFolder = queue(queue.count)
          queue.Remove(queue.count) 'dequeue
          '...insert any folder processing code here...'
         '*...(Here I write the name of the folder to the excel sheet)*.
          For Each oSubfolder In oFolder.SubFolders
              queue.Add oSubfolder 'enqueue
          Next oSubfolder
          For Each oFile In oFolder.Files
         '...insert any file processing code here...
         Next oFile
      Loop
  
  End Sub
  </code></pre>
  
  <p>I've tried the "LIFO" version (as above) and the "FIFO" version, but neither of them produces a standard alphabetical listing. The above version lists the drive in exact reverse alphabetical order, and the "FIFO" version produces a list in normal alphabetical order, but it lists only the first-level folders, then starts again and lists all the second-level folders, again in alphabetical order, then the third level of folders, again starting over from "A", etc. As a result, the subfolders are not listed under their parent folder.</p>
  
  <p>Does anyone know what I can do to get a standard tree structure, in alphabetical order by folder and subfolder name?</p>
  
  <p>TIA</p>
  
  <p>Les</p>
  
  <p>Update: for some reason I can't manage to show all the comments on this thread or write a new comment. But I wanted to thank everybody, in particular @Rosenfeld, and say that I'm eager to try the solution using <code>dir</code> but am currently swamped with work. I'll report back in a few days when I get a chance to stumble around.</p>