Recursive filename search I have created a little program to search a set of folders holding documents scanned.

The folder structure is as follows:


  c:\images\year\month\date\documenttype\firstpartofdocumentNo.\
  
  the year folder contains years from 2005 - 2015
  
  the month folder contains the months of the year (Obviously) same with
  date
  
  the documenttype folder can contain between 1 and 5 folders
  
  the firstpartofdocumentno. can contain between 1 and 3 folders


The code I am using at the moment is:

CompName = Environment.MachineName

TicketNo = TxtTicketNo.Text

If CompName = "Comp1" Then
    ImageDir = "C:\Images\"
Else
    ImageDir = "\\Comp2\Images\"
End If

For Each DirYear As String In Directory.GetDirectories(ImageDir)
    Dim YearInfo As New DirectoryInfo(DirYear)

    For Each DirMonth As String In Directory.GetDirectories(DirYear)
        Dim MonthInfo As New DirectoryInfo(DirMonth)

        For Each DirDate As String In Directory.GetDirectories(DirMonth)
            Dim DateInfo As New DirectoryInfo(DirDate)

            For Each DirType As String In Directory.GetDirectories(DirDate)
                Dim TypeInfo As New DirectoryInfo(DirType)

                For Each DirStart As String In Directory.GetDirectories(DirType)
                    Dim StartInfo As New DirectoryInfo(DirStart)

                    MainDirectory = ImageDir &amp; YearInfo.Name &amp; "\" &amp; MonthInfo.Name &amp; "\" &amp; DateInfo.Name &amp; "\" &amp; TypeInfo.Name &amp; "\" &amp; StartInfo.Name &amp; "\"
                                                                                            For Each Ticket As String In Directory.GetFiles(MainDirectory, TicketNo &amp; "*")
                        LstFiles.Items.Add(Ticket)
                    Next

                Next

             Next

        Next
    Next
   Next


I have a textbox on the form which is used to enter the last four numbers of the ticketno and then this code runs when the button is clicked.

The problem is it can take up to five minutes to search, so I was wondering if there is a way to optimize this code to speed it up a bit or does this sound about right for searching that many folders.
This might sound like a goofy answer, but if you already have Windows' file indexing service set up and running on your system, you can search the database contents of the index results using simple SQL terminology. 

Here is an article that shows you how

In this way, you're not traversing the directory tree each time you search, but rather you're letting Windows Indexing Service log each new file when it is entered, and then searching the resulting database of information.