473,490 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Better way to process many conditions with If Then

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
Sep 5 '06 #1
7 1057
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
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

Sep 5 '06 #2
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
Sep 5 '06 #3
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/is...dvancedBasics/ 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

Sep 5 '06 #4
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

"methmonster" <pe********@rci.rogers.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.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

Sep 5 '06 #5
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

"S Kachru" <re************************@hotmail.comwrote in message
news:79**************************@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/is...dvancedBasics/ 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("APPLICATIO N 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


Sep 5 '06 #6
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?
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
Sep 5 '06 #7
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

Sep 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
4502
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
0
1677
by: adavidso | last post by:
I am a bit of a newbie to python, and would like a little advice on how to more efficiently process arrays. I work a lot with satellite data, and have opened and read two binary images into...
3
2255
by: mrhicks | last post by:
Hello all, I have a question regarding efficeny and how to find the best approach when trying to find flag with in a structure of bit fields. I have several structures which look similar to ...
16
8466
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
7
1124
by: Phl | last post by:
hi, just wondering which way of coding is considered better? This style where the two returns aren't interconnected by an else statement in the if seems to be very popular these day, where use...
39
3185
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
2
4088
by: serge | last post by:
/* Subject: How best to use BETWEEN Begin and End Dates to find out if an employee was/is member of any group for a certain date range? You can copy/paste this whole post in SQL Query Analyzer...
4
21267
by: Hexman | last post by:
Hello All, I'd like to find out the best way to add a cb column to a dgv and process efficiently. I see at least two ways of doing it. ------------------------------- 1) Add a cb to the dgv,...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.