Better way to process many conditions with If Then

  • Thread starter Thread starter Chris
  • Start date Start date
C

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
 
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
 
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.
 
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...
 
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
 
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
 
yeah keep it in a database and then load from the database at runtime.

SHIT.

what do you still use INI files?

ROFL

XML?

hahahahaha what happens when an end user needs to update it?
 
The more polite way of saying this is:

Load your list of strings to search by from another source.
Don't hardcode in the application.
Perhaps put into an XML doc, or if you are using a database, create a
table to hold the list of bad file strings. If using 2.0 you can use
your app.config (or web.config) for this as well. Or just a plain old
comma delimited text file.

That solution aside, if its just a list of strings, why not a string
array?
Why use a generic list like others suggested? Seems like overkill, but
I haven't done any performance measures to back up that statement.

' Note: Code untested
Dim sr as new streamreader("badfiles.txt")
dim badfiles() as string = sr.ReadToEnd().Split(",".tocharArray())
sr.close()

for each badfile as string in badfiles, etc etc
 
Back
Top