Home All Groups Group Topic Archive Search About

Better way to process many conditions with If Then

Author
5 Sep 2006 7:27 PM
Chris
I'm writing an application that does a scan on all the files for a given
drive letter.

Ideally what I want is to have a list of keywords that my program will
ignore and not scan.

For example:

For Each fileInfo As FileInfo In FilesArray
    If fileInfo.FullName.ToUpper.Contains( a keyword in the list of
keywords ) then
    'do nothing
    Else
    'do something important
    End If
Next

As compared to the following mess that I have now:

For Each fileInfo As FileInfo In FilesArray
                'Do not look at files that cause lots of noise and are of no
intrest
                If fileInfo.FullName.ToUpper.Contains("RECYCLER") Or _
                fileInfo.FullName.ToUpper.Contains("WINDOWS") Or _
                fileInfo.FullName.ToUpper.Contains("WINNT") Or _
                fileInfo.FullName.ToUpper.Contains("LOG") Or _
                fileInfo.FullName.ToUpper.Contains("DUMP") Or _
                fileInfo.FullName.ToUpper.Contains("APPLICATION DATA") Or _
                fileInfo.FullName.ToUpper.Contains("SETUP") Or _
                fileInfo.FullName.ToUpper.Contains("LOCAL SETTINGS") Or _
                fileInfo.FullName.ToUpper.Contains("COOKIES") Then
                    'Do nothing
                Else
                    'We have a file of intrest
                    'Do something with it
Next

The list that I test against is actually about 10 times longer than the
above list. I'm just thinking that there has to be a more readable/compact
way of doing this.

I also need the feature to dynamically add/remove items from the "ignore"
list, which makes me think of a using a collection.

Thanks for any help,

Chris

Author
5 Sep 2006 8:17 PM
GhostInAK
Hello Chris,

You answered your own question at the last there.. a collection is a fine
solution.  For each File in TileArray, For each word in BadWords, if comparision
succeeds then do something..

-Boo

Show quoteHide quote
> I'm writing an application that does a scan on all the files for a
> given drive letter.
>
> Ideally what I want is to have a list of keywords that my program will
> ignore and not scan.
>
> For example:
>
> For Each fileInfo As FileInfo In FilesArray
> If fileInfo.FullName.ToUpper.Contains( a keyword in the list of
> keywords ) then
> 'do nothing
> Else
> 'do something important
> End If
> Next
> As compared to the following mess that I have now:
>
> For Each fileInfo As FileInfo In FilesArray
> 'Do not look at files that cause lots of noise and are
> of no
> intrest
> If fileInfo.FullName.ToUpper.Contains("RECYCLER") Or _
> fileInfo.FullName.ToUpper.Contains("WINDOWS") Or _
> fileInfo.FullName.ToUpper.Contains("WINNT") Or _
> fileInfo.FullName.ToUpper.Contains("LOG") Or _
> fileInfo.FullName.ToUpper.Contains("DUMP") Or _
> fileInfo.FullName.ToUpper.Contains("APPLICATION DATA")
> Or _
> fileInfo.FullName.ToUpper.Contains("SETUP") Or _
> fileInfo.FullName.ToUpper.Contains("LOCAL SETTINGS")
> Or _
> fileInfo.FullName.ToUpper.Contains("COOKIES") Then
> 'Do nothing
> Else
> 'We have a file of intrest
> 'Do something with it
> Next
> The list that I test against is actually about 10 times longer than
> the above list. I'm just thinking that there has to be a more
> readable/compact way of doing this.
>
> I also need the feature to dynamically add/remove items from the
> "ignore" list, which makes me think of a using a collection.
>
> Thanks for any help,
>
> Chris
>
Author
5 Sep 2006 8:23 PM
methmonster
You could try something like the following:
        Dim myArray As New ArrayList
        myArray.Add("BADFILE1")
        myArray.Add("BADFILE2")
        myArray.Add("BADFILE3")

        For Each myFileInfo As FileInfo In FilesArray
            If Not myArray.Contains(FileInfo.FullName.ToUpper) Then
                'do something
            End If
        Next

And if you're creative you could also build myArray from a text or XML
file.  Note: I prefer using the 'Not' operator in my if statements
rather than having a 'Do Nothing' section of code, but you can remove
it if you prefer.

Chris wrote:
Show quoteHide quote
> I'm writing an application that does a scan on all the files for a given
> drive letter.
>
> Ideally what I want is to have a list of keywords that my program will
> ignore and not scan.
>
> For example:
>
> For Each fileInfo As FileInfo In FilesArray
>     If fileInfo.FullName.ToUpper.Contains( a keyword in the list of
> keywords ) then
>     'do nothing
>     Else
>     'do something important
>     End If
> Next
>
> As compared to the following mess that I have now:
>
> For Each fileInfo As FileInfo In FilesArray
>                 'Do not look at files that cause lots of noise and are of no
> intrest
>                 If fileInfo.FullName.ToUpper.Contains("RECYCLER") Or _
>                 fileInfo.FullName.ToUpper.Contains("WINDOWS") Or _
>                 fileInfo.FullName.ToUpper.Contains("WINNT") Or _
>                 fileInfo.FullName.ToUpper.Contains("LOG") Or _
>                 fileInfo.FullName.ToUpper.Contains("DUMP") Or _
>                 fileInfo.FullName.ToUpper.Contains("APPLICATION DATA") Or _
>                 fileInfo.FullName.ToUpper.Contains("SETUP") Or _
>                 fileInfo.FullName.ToUpper.Contains("LOCAL SETTINGS") Or _
>                 fileInfo.FullName.ToUpper.Contains("COOKIES") Then
>                     'Do nothing
>                 Else
>                     'We have a file of intrest
>                     'Do something with it
> Next
>
> The list that I test against is actually about 10 times longer than the
> above list. I'm just thinking that there has to be a more readable/compact
> way of doing this.
>
> I also need the feature to dynamically add/remove items from the "ignore"
> list, which makes me think of a using a collection.
>
> Thanks for any help,
>
> Chris
Author
5 Sep 2006 8:31 PM
S Kachru
Or this...

Module Module1

    Private _fileName As String
    Sub Main()
        Dim lst As New System.Collections.Generic.List(Of String)
        lst.Add("RECYCLER")
        lst.Add("LOG")
        lst.Add("WINNT")
        '...
        '...
        _fileName = "WINNT_123"

        Dim match As String = lst.Find(AddressOf Matches)
        If Not String.IsNullOrEmpty(match) Then
            MsgBox("found match : " & match)
        Else
            MsgBox("no match")
        End If

    End Sub

    Private Function Matches(ByVal listElement As String) As Boolean
        Return _fileName.Contains(listElement)
    End Function
End Module

See http://msdn.microsoft.com/msdnmag/issues/06/09/AdvancedBasics/  for explanation...


Show quoteHide quote
> You could try something like the following:
> Dim myArray As New ArrayList
> myArray.Add("BADFILE1")
> myArray.Add("BADFILE2")
> myArray.Add("BADFILE3")
> For Each myFileInfo As FileInfo In FilesArray
> If Not myArray.Contains(FileInfo.FullName.ToUpper) Then
> 'do something
> End If
> Next
> And if you're creative you could also build myArray from a text or XML
> file.  Note: I prefer using the 'Not' operator in my if statements
> rather than having a 'Do Nothing' section of code, but you can remove
> it if you prefer.
>
> Chris wrote:
>
>> I'm writing an application that does a scan on all the files for a
>> given drive letter.
>>
>> Ideally what I want is to have a list of keywords that my program
>> will ignore and not scan.
>>
>> For example:
>>
>> For Each fileInfo As FileInfo In FilesArray
>> If fileInfo.FullName.ToUpper.Contains( a keyword in the list of
>> keywords ) then
>> 'do nothing
>> Else
>> 'do something important
>> End If
>> Next
>> As compared to the following mess that I have now:
>>
>> For Each fileInfo As FileInfo In FilesArray
>> 'Do not look at files that cause lots of noise and are of no
>> intrest
>> If fileInfo.FullName.ToUpper.Contains("RECYCLER") Or _
>> fileInfo.FullName.ToUpper.Contains("WINDOWS") Or _
>> fileInfo.FullName.ToUpper.Contains("WINNT") Or _
>> fileInfo.FullName.ToUpper.Contains("LOG") Or _
>> fileInfo.FullName.ToUpper.Contains("DUMP") Or _
>> fileInfo.FullName.ToUpper.Contains("APPLICATION DATA") Or _
>> fileInfo.FullName.ToUpper.Contains("SETUP") Or _
>> fileInfo.FullName.ToUpper.Contains("LOCAL SETTINGS") Or _
>> fileInfo.FullName.ToUpper.Contains("COOKIES") Then
>> 'Do nothing
>> Else
>> 'We have a file of intrest
>> 'Do something with it
>> Next
>> The list that I test against is actually about 10 times longer than
>> the above list. I'm just thinking that there has to be a more
>> readable/compact way of doing this.
>>
>> I also need the feature to dynamically add/remove items from the
>> "ignore" list, which makes me think of a using a collection.
>>
>> Thanks for any help,
>>
>> Chris
>>
Author
5 Sep 2006 9:29 PM
Chris
Thanks S Kachru,

Awesome article. With a little tweaking this looks like exactly like what I
want.

I needed to finally take a look at generics, I'm sure I'm missing out on a
lot by not using them.

Thanks again,
Chris

Show quoteHide quote
"S Kachru" <removemecyberdj123remov***@hotmail.com> wrote in message
news:79b12b67279a68c89f1fe703157a@msnews.microsoft.com...
> Or this...
>
> Module Module1
>
>    Private _fileName As String
>    Sub Main()
>        Dim lst As New System.Collections.Generic.List(Of String)
>        lst.Add("RECYCLER")
>        lst.Add("LOG")
>        lst.Add("WINNT")
>        '...
>        '...
>        _fileName = "WINNT_123"
>
>        Dim match As String = lst.Find(AddressOf Matches)
>        If Not String.IsNullOrEmpty(match) Then
>            MsgBox("found match : " & match)
>        Else
>            MsgBox("no match")
>        End If
>
>    End Sub
>
>    Private Function Matches(ByVal listElement As String) As Boolean
>        Return _fileName.Contains(listElement)
>    End Function
> End Module
>
> See http://msdn.microsoft.com/msdnmag/issues/06/09/AdvancedBasics/  for
> explanation...
>
>
>> You could try something like the following:
>> Dim myArray As New ArrayList
>> myArray.Add("BADFILE1")
>> myArray.Add("BADFILE2")
>> myArray.Add("BADFILE3")
>> For Each myFileInfo As FileInfo In FilesArray
>> If Not myArray.Contains(FileInfo.FullName.ToUpper) Then
>> 'do something
>> End If
>> Next
>> And if you're creative you could also build myArray from a text or XML
>> file.  Note: I prefer using the 'Not' operator in my if statements
>> rather than having a 'Do Nothing' section of code, but you can remove
>> it if you prefer.
>>
>> Chris wrote:
>>
>>> I'm writing an application that does a scan on all the files for a
>>> given drive letter.
>>>
>>> Ideally what I want is to have a list of keywords that my program
>>> will ignore and not scan.
>>>
>>> For example:
>>>
>>> For Each fileInfo As FileInfo In FilesArray
>>> If fileInfo.FullName.ToUpper.Contains( a keyword in the list of
>>> keywords ) then
>>> 'do nothing
>>> Else
>>> 'do something important
>>> End If
>>> Next
>>> As compared to the following mess that I have now:
>>>
>>> For Each fileInfo As FileInfo In FilesArray
>>> 'Do not look at files that cause lots of noise and are of no
>>> intrest
>>> If fileInfo.FullName.ToUpper.Contains("RECYCLER") Or _
>>> fileInfo.FullName.ToUpper.Contains("WINDOWS") Or _
>>> fileInfo.FullName.ToUpper.Contains("WINNT") Or _
>>> fileInfo.FullName.ToUpper.Contains("LOG") Or _
>>> fileInfo.FullName.ToUpper.Contains("DUMP") Or _
>>> fileInfo.FullName.ToUpper.Contains("APPLICATION DATA") Or _
>>> fileInfo.FullName.ToUpper.Contains("SETUP") Or _
>>> fileInfo.FullName.ToUpper.Contains("LOCAL SETTINGS") Or _
>>> fileInfo.FullName.ToUpper.Contains("COOKIES") Then
>>> 'Do nothing
>>> Else
>>> 'We have a file of intrest
>>> 'Do something with it
>>> Next
>>> The list that I test against is actually about 10 times longer than
>>> the above list. I'm just thinking that there has to be a more
>>> readable/compact way of doing this.
>>>
>>> I also need the feature to dynamically add/remove items from the
>>> "ignore" list, which makes me think of a using a collection.
>>>
>>> Thanks for any help,
>>>
>>> Chris
>>>
>
>
Author
5 Sep 2006 9:27 PM
Chris
Thanks Meth Monster,

The only problem I see with this is that
myArray.Contains(FileInfo.FullName.ToUpper) will never return true as I need
to fill myArray with keywords/parts of file names. However, maybe I'm
misunderstanding something or could have been more clear with the question.

Thanks again,

Chris

Show quoteHide quote
"methmonster" <peter.m***@rci.rogers.com> wrote in message
news:1157487818.822008.202660@e3g2000cwe.googlegroups.com...
> You could try something like the following:
>        Dim myArray As New ArrayList
>        myArray.Add("BADFILE1")
>        myArray.Add("BADFILE2")
>        myArray.Add("BADFILE3")
>
>        For Each myFileInfo As FileInfo In FilesArray
>            If Not myArray.Contains(FileInfo.FullName.ToUpper) Then
>                'do something
>            End If
>        Next
>
> And if you're creative you could also build myArray from a text or XML
> file.  Note: I prefer using the 'Not' operator in my if statements
> rather than having a 'Do Nothing' section of code, but you can remove
> it if you prefer.
>
> Chris wrote:
>> I'm writing an application that does a scan on all the files for a given
>> drive letter.
>>
>> Ideally what I want is to have a list of keywords that my program will
>> ignore and not scan.
>>
>> For example:
>>
>> For Each fileInfo As FileInfo In FilesArray
>>     If fileInfo.FullName.ToUpper.Contains( a keyword in the list of
>> keywords ) then
>>     'do nothing
>>     Else
>>     'do something important
>>     End If
>> Next
>>
>> As compared to the following mess that I have now:
>>
>> For Each fileInfo As FileInfo In FilesArray
>>                 'Do not look at files that cause lots of noise and are of
>> no
>> intrest
>>                 If fileInfo.FullName.ToUpper.Contains("RECYCLER") Or _
>>                 fileInfo.FullName.ToUpper.Contains("WINDOWS") Or _
>>                 fileInfo.FullName.ToUpper.Contains("WINNT") Or _
>>                 fileInfo.FullName.ToUpper.Contains("LOG") Or _
>>                 fileInfo.FullName.ToUpper.Contains("DUMP") Or _
>>                 fileInfo.FullName.ToUpper.Contains("APPLICATION DATA") Or
>> _
>>                 fileInfo.FullName.ToUpper.Contains("SETUP") Or _
>>                 fileInfo.FullName.ToUpper.Contains("LOCAL SETTINGS") Or _
>>                 fileInfo.FullName.ToUpper.Contains("COOKIES") Then
>>                     'Do nothing
>>                 Else
>>                     'We have a file of intrest
>>                     'Do something with it
>> Next
>>
>> The list that I test against is actually about 10 times longer than the
>> above list. I'm just thinking that there has to be a more
>> readable/compact
>> way of doing this.
>>
>> I also need the feature to dynamically add/remove items from the "ignore"
>> list, which makes me think of a using a collection.
>>
>> Thanks for any help,
>>
>> Chris
>