Click to See Complete Forum and Search --> : [2003] Remove Items Read from file, from ListBox


GrimmReaper
October 15th, 2009, 04:12 AM
Hello, me again...

OK, straight to the point.

I write folder names to a file like this :
Private Sub WriteExcludeFolders()
Dim SelFolders(clbExclude.CheckedItems.Count - 1) As String

Dim i As Integer


For i = 0 To clbExclude.CheckedItems.Count - 1
SelFolders(i) = clbExclude.CheckedItems(i)

Next i

If File.Exists(Application.StartupPath & "ExcludedFolders.txt") Then
File.Delete(Application.StartupPath & "ExcludedFolders.txt")
End If

srFile = New IO.StreamWriter("ExcludedFolders.txt")

For i = 0 To SelFolders.Length - 1
srFile.WriteLine(WallDir & "\" & SelFolders(i))

Next

srFile.Close()
End Sub

As you can see, it is folders I'd like to exclude from file searches.

Now, when my program launches, I do this :
Private Function ReadExcludeFolds(ByVal filePath As String) As String()

Dim sr As System.IO.StreamReader
Try
sr = New System.IO.StreamReader(filePath)
Return System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd, "\r\n")
Finally
If Not sr Is Nothing Then sr.Close()
End Try


End Function

Private Sub LoadExcludeFolds()
Try
ExcludeLines = ReadExcludeFolds("ExcludedFolders.txt")
Dim ExcludeLine As String
Dim ItemsFound As String
Dim ItemCount As Integer = lstFound.Items.Count '- 1
Dim I As Integer


For I = 0 To ItemCount - 1
For Each ExcludeLine In ExcludeLines
ItemCount -= 1 ' lstFound.Items.Count - 1
If I >= ItemCount Then Exit For

ItemsFound = lstFound.Items(I).ToString().Substring(0, lstFound.Items(I).ToString().LastIndexOf("\"))
If ExcludeLine.Equals(ItemsFound) Then


lstFound.Items.RemoveAt(I)
NumFiles -= 1

lblNumFiles.Text = NumFiles.ToString() & " Items In Repository"
End If
Next
Next I


Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub
What is supposed to happen is:
My program launches, I call the LoadExcludeFolds sub. This should read the contents of the textfile, and if it finds a folder listed in it, in the listbox, it should remove that item.

This only removes a few items.

So, say I have this in my text file :
C:\Testing\Images\Cars

And i have this in my listbox
C:\Testing\Images\Cars\BMW.jpg
C:\Testing\Images\Cars\Mercedes.jpg
C:\Testing\Images\Cars\Audi.jpg
C:\Testing\Images\Cars\Mazda.jpg

It will only remove BMW and Mercedes.

Any advice, any other way of doing this??

HanneSThEGreaT
October 21st, 2009, 12:39 AM
I would change :
For I = 0 To ItemCount - 1
For Each ExcludeLine In ExcludeLines
ItemCount -= 1 ' lstFound.Items.Count - 1
If I >= ItemCount Then Exit For

ItemsFound = lstFound.Items(I).ToString().Substring(0, lstFound.Items(I).ToString().LastIndexOf("\"))
If ExcludeLine.Equals(ItemsFound) Then


lstFound.Items.RemoveAt(I)
NumFiles -= 1

lblNumFiles.Text = NumFiles.ToString() & " Items In Repository"
End If
Next
Next I

To :

For I = (ItemCount - 1) To 0 Step -1
For Each ExcludeLine In ExcludeLines
ItemCount -= 1

ItemsFound = lstFound.Items(I).ToString().Substring(0, lstFound.Items(I).ToString().LastIndexOf("\"))
If ExcludeLine.Equals(ItemsFound) Then

lstFound.Items.RemoveAt(I)

NumFiles -= 1

lblNumFiles.Text = NumFiles.ToString() & " Items In Repository"
Exit For
End If
Next
Next I

The trick is to loop backwards in the listbox