Results 1 to 2 of 2

Thread: Search exception filter

  1. #1

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    Search exception filter

    Hello people,

    I have made following code that gather each path for all folders and subfolders in a certain path.
    my result generates approx 6000 different items.

    Now I need to exclude some folders.

    My first question:
    is the fastest way to make a list of items that is exceptions in my searchfunction of what I am not intrested in?

    because I have got several folders I don't want to find and its always the same pattern.
    if how?

    this is my code

    Code:
        Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            For Each d In Directory.EnumerateDirectories("C:\", "*.*", SearchOption.AllDirectories)
                Dim myPath = ((d.ToString()))
    
                Console.WriteLine(myPath)
            Next
        End Sub
    or will I add all items as they are to a new list (this take ages years!) then clean unwanted items from the list.

    Here is an example of my result from the code:


    C:\Archive\custommers\original-files\32001-33000\32100\32099\
    C:\Archive\custommers\original-files\32001-33000\32100\32099\hello\
    C:\Archive\custommers\original-files\32001-33000\32100\32099\dontuseme\
    ............. and so on........

    C:\Archive\custommers\original-files\33001-34000\33100\33099\
    C:\Archive\custommers\original-files\33001-34000\32100\33099\hello\
    C:\Archive\custommers\original-files\33001-34000\32100\33099\dontuseme\

    .............and so on......


    and so on.....

    Could someone help me?

    Thank you in advance !

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Search exception filter

    Intuitively, it's probably faster to write a loop that looks like:
    Code:
    For Each directoryPath In Directory.EnumerateDirectories(...)
        If IsGood(directoryPath)
            Console.WriteLine(myPath)
        End If
    Next
    This only has to go over the list once. If you do it the other way, you have to iterate at least twice: once to build the list, then a second time to remove items you don't want. But it's also trickier, because iterating over a list while you are changing it takes a little more care.

    I think your second question is "How do I find folders that match a certain pattern?" That'll depend on the pattern, but a Regex is probably a workable choice here. For example, this function would enumerate and reject the paths that end with "dontuseme":
    Code:
    Sub PerformEnumeration()
        For Each directoryPath In Directory.EnumerateDirectories(...)
            If IsGood(directoryPath)
                Console.WriteLine(myPath)
            End If
        Next
    End Sub
    
    Function IsGood(ByVal directoryPath as String) As Boolean
        Dim badPattern = "\\\d+\\dontuseme\\$"
        Return Not System.Text.RegularExpressions.IsMatch(directoryPath, badPattern)
    End Function
    That regular expression says "Match strings that end with a \ followed by any number of digits, another \, the string "dontuseme", and one more backslash". They are convenient for matching certain strings, but there might be better ways if your pattern is more complex.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width