Help Needed: Strange Results

Taylor

New member
Joined
Apr 28, 2009
Messages
2
Programming Experience
Beginner
hey guys,

Been browsing these forums for a few months when I'm stuck on problems but this one has really got me stumped. Perhaps I'm missing something simple though :)

Anyway, I've been tasked to develop a program that will delete lines from a .ini file were needed. However when I run the program (restoring the orginal files each time) I get different results each time.
For Instance, sometimes it will delete 1100 lines through all of the .ini files in the folder, then other times it will delete 600. I can run through multiple times and eventually it will slowly go down to0. But I can't see why it doesn't do it the first time round.

Heres the code:
VB.NET:
Imports System.IO
Public Class frmFail
    Dim files() As String
    Dim path As String
    Dim line As String
    Dim lineCount As Integer
    Dim s() As String
    Dim splitLine As String
    Dim x As Integer
    Dim check As Integer
    Dim deletedItems As Integer = 0
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim valid As Boolean = False

        Try
            files = Directory.GetFiles(txtDirect.Text, "*.ini")
        Catch ex As DirectoryNotFoundException
            MessageBox.Show("Directory Not Found, please check correct path is entered", "Error")
            Exit Sub
        Catch moarEx As ArgumentException
            MessageBox.Show("No path entered", "Error")
            Exit Sub
        End Try

        If MessageBox.Show("Are you sure you want to continue,? Deleted items are NOT recoverable.", _
                           "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        If txtDirect.Text = "" Then
            MessageBox.Show("No Directory Entered, please enter path at top of window", "Error")
            Exit Sub
        End If



        deletedItems = 0
        btnLoad.Enabled = False
        btbDirect.Enabled = False

        chbInstall.Enabled = False
        chbUninstall.Enabled = False
        chbDepend.Enabled = False
        chbVersion.Enabled = False

        lstShow.Items.Clear()
        lineCount = 0
        Dim SR As StreamReader

        If chbInstall.Checked = True And chbUninstall.Checked = True And chbDepend.Checked = True And chbVersion.Checked = True Then
            check = 1 'if all are checked
        ElseIf chbInstall.Checked = True And chbUninstall.Checked = False And chbDepend.Checked = True And chbVersion.Checked = False Then
            check = 2 'if install is checked
        ElseIf chbInstall.Checked = False And chbUninstall.Checked = True And chbDepend.Checked = True And chbVersion.Checked = False Then
            check = 3 'if uninstall if checked
        ElseIf chbDepend.Checked = True And chbInstall.Checked = False And chbUninstall.Checked = False And chbVersion.Checked = False Then
            check = 4 'if depend is checked
        ElseIf chbInstall.Checked = True And chbUninstall.Checked = True And chbDepend.Checked = False And chbVersion.Checked = False Then
            check = 5 'if install and uninstall are checked
        ElseIf chbInstall.Checked = True And chbUninstall.Checked = False And chbDepend.Checked = True And chbVersion.Checked = False Then
            check = 6 'if install and depend are checked
        ElseIf chbInstall.Checked = False And chbUninstall.Checked = True And chbDepend.Checked = True And chbVersion.Checked = False Then
            check = 7 'if uninstall and depend are checked
        ElseIf chbInstall.Checked = False And chbUninstall.Checked = False And chbDepend.Checked = False And chbVersion.Checked = True Then
            check = 8 'if  version is checked
        ElseIf chbInstall.Checked = True And chbUninstall.Checked = False And chbDepend.Checked = False And chbVersion.Checked = True Then
            check = 9 'if install and version are checked
        ElseIf chbInstall.Checked = False And chbUninstall.Checked = True And chbDepend.Checked = False And chbVersion.Checked = True Then
            check = 10 'if uninstall and version are checked
        ElseIf chbInstall.Checked = False And chbUninstall.Checked = False And chbDepend.Checked = True And chbVersion.Checked = True Then
            check = 11 'if depend and version are checked
        ElseIf chbInstall.Checked = True And chbUninstall.Checked = True And chbDepend.Checked = True And chbVersion.Checked = False Then
            check = 12 'if install,uninstall,depend are checked
        ElseIf chbInstall.Checked = False And chbUninstall.Checked = True And chbDepend.Checked = True And chbVersion.Checked = True Then
            check = 13 'if uninstall,depend,version  are checked
        ElseIf chbInstall.Checked = True And chbUninstall.Checked = True And chbDepend.Checked = False And chbVersion.Checked = True Then
            check = 14 'if install,uninstall,version  are checked
        ElseIf chbInstall.Checked = True And chbUninstall.Checked = False And chbDepend.Checked = True And chbVersion.Checked = True Then
            check = 15 'if install,depend,version  are checked
        Else
            MessageBox.Show("Please Check At Least One Option", "Error")
        End If

        For x = 0 To UBound(files)
            lineCount = 0
            lstShow.Items.Clear()

            SR = New StreamReader(files(x))
            Do
                lineCount += 1
                line = SR.ReadLine
            Loop Until line Is Nothing
            lineCount -= 2
            SR.Dispose()
            SR.Close()
            SR = Nothing

            SR = New StreamReader(files(x))
            line = SR.ReadLine

            For i = 0 To lineCount
                lstShow.Items.Add(line)
                line = SR.ReadLine
            Next
            SR.Dispose()
            SR.Close()
            SR = Nothing

            lstShow.SelectedIndex = 0

            For i = 0 To lineCount
                Dim search As String = lstShow.Items(lstShow.SelectedIndex)
                s = search.Split("=")
                If UBound(s) > 0 Then
                    Select Case check
                        Case 1
                            If s(1) = "INSTALL_FAILED" Or s(1) = "UNINSTALL_FAILED" Or s(1) = "NOT_INSTALLED (DEPENDENCY)" Or s(1) = "NOT_INSTALL (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 2
                            If s(1) = "INSTALL_FAILED" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 3
                            If s(1) = "UNINSTALL_FAILED" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 4
                            If s(1) = "NOT_INSTALLED (DEPENDENCY)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 5
                            If s(1) = "INSTALL_FAILED" Or s(1) = "UNINSTALL_FAILED" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 6
                            If s(1) = "INSTALL_FAILED" Or s(1) = "NOT_INSTALLED (DEPENDENCY)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 7
                            If s(1) = "UNINSTALL_FAILED" Or s(1) = "NOT_INSTALLED (DEPENDENCY)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 8
                            If s(1) = "NOT_INSTALLED (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 9
                            If s(1) = "INSTALL_FAILED" Or s(1) = s(1) = "NOT_INSTALLED (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 10
                            If s(1) = "UNINSTALL_FAILED" Or s(1) = s(1) = "NOT_INSTALLED (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 11
                            If s(1) = "NOT_INSTALLED (DEPENDENCY)" Or s(1) = "NOT_INSTALLED (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 12
                            If s(1) = "INSTALL_FAILED" Or s(1) = "UNINSTALL_FAILED" Or s(1) = "NOT_INSTALLED (DEPENDENCY)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 13
                            If s(1) = "UNINSTALL_FAILED" Or s(1) = "NOT_INSTALLED (DEPENDENCY)" Or s(1) = "NOT_INSTALLED (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 14
                            If s(1) = "INSTALL_FAILED" Or s(1) = "UNINSTALL_FAILED" Or s(1) = "NOT_INSTALLED (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case 15
                            If s(1) = "INSTALL_FAILED" Or s(1) = "NOT_INSTALLED (DEPENDENCY)" Or s(1) = "NOT_INSTALLED (OS VERSION)" Then
                                valid = True
                            Else
                                valid = False
                            End If
                        Case Else
                            valid = False
                    End Select
                End If
                Try
                    If valid = True Then
                        lstShow.Items.RemoveAt(lstShow.SelectedIndex)
                        lineCount -= 1
                        i = 0
                        lstShow.SelectedIndex = 0
                        deletedItems += 1
                        lblDel.Text = CStr(deletedItems) & " Items Deleted"
                    End If
                    If lstShow.SelectedIndex < lineCount Then lstShow.SelectedIndex += 1
                Catch ex As Exception
                    MsgBox(ex.Message & " " & files(x) & " " & i)
                End Try

            Next


            Dim write(lineCount) As String
            lstShow.SelectedIndex = 0

            For i = 0 To lineCount
                write(i) = lstShow.Items(lstShow.SelectedIndex)
                If lstShow.SelectedIndex < lineCount Then lstShow.SelectedIndex += 1
            Next

            Dim stream As New FileStream(files(x), FileMode.Open, FileAccess.Write, FileShare.None)
            Dim writer As New StreamWriter(stream)

            For i = 0 To UBound(write)
                writer.WriteLine(write(i))
            Next
            writer.Close()
            writer = Nothing

        Next

        btnLoad.Enabled = True
        btbDirect.Enabled = True

        chbInstall.Enabled = True
        chbUninstall.Enabled = True
        chbDepend.Enabled = True
        chbVersion.Enabled = True
    End Sub

    Private Sub btbDirect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbDirect.Click
        Try
            files = Directory.GetFiles(txtDirect.Text, "*.ini")
        Catch ex As DirectoryNotFoundException
            MessageBox.Show("Directory Not Found, please check correct path is entered", "Error")
            Exit Sub
        Catch moarEx As ArgumentException
            MessageBox.Show("No path entered", "Error")
            Exit Sub
        End Try

        lstShow.Items.Clear()
        For x = 0 To UBound(files)
            lstShow.Items.Add(files(x))
        Next
    End Sub

    Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click
        frmAbout.Show()
    End Sub
End Class

Hopefully I've explained myself properly. The first if statement is just see what combination of check boxes are ticked. Through all runs all are checked.

Thanks,
/Taylor
 
Strange results? Talk about strange code... ;) There is no dependency between your search options, so no need to check combinations (which could be done much better with a flags Enum if you needed to). All those "Else valid = False" also really add to the volume of your code, you wouldn't need any of them if you needed any of that code. More over, you're doing a lot of UI interaction that is not needed here, this is something that finish processing in a split second, you can do your processing, then update UI later if you wish. Here's suggested code that fixes the mentioned issues, a complete rewrite actually that does the same your code appeared to be doing:
VB.NET:
Private Sub ProcessFiles()
    'get folder
    If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then Return

    'get search options
    Dim searchStrings As New List(Of String)
    If chbInstall.Checked Then
        searchStrings.Add("INSTALL_FAILED")
    End If
    If chbUninstall.Checked Then
        searchStrings.Add("UNINSTALL_FAILED")
    End If
    If chbDepend.Checked Then
        searchStrings.Add("NOT_INSTALLED (DEPENDENCY)")
    End If
    If chbVersion.Checked Then
        searchStrings.Add("NOT_INSTALL (OS VERSION)")
    End If

    'validate search options
    If searchStrings.Count = 0 Then
        MessageBox.Show("Please Check At Least One Option", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return
    End If

    'confirm user
    If MessageBox.Show("Are you sure you want to continue,? Deleted items are NOT recoverable.", _
                       "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.No Then
        Exit Sub
    End If

    'process files
    For Each file As String In IO.Directory.GetFiles(Me.FolderBrowserDialog1.SelectedPath, "*.ini")
        processFile(file, searchStrings.ToArray)
    Next
End Sub
VB.NET:
Private Sub ProcessFile(ByVal file As String, ByVal search() As String)
    Dim newLines As New List(Of String)
    Dim removedLines As New List(Of String)
    For Each line As String In IO.File.ReadAllLines(file)
        Dim tmp() As String = line.Split("="c)
        If tmp.Length >= 2 Then
            If search.Contains(tmp(1)) Then
                removedLines.Add(line)
            Else
                newLines.Add(line)
            End If
        End If
    Next
    IO.File.WriteAllLines(file, newLines.ToArray)
    Dim removedPath As String = IO.Path.ChangeExtension(file, ".removed")
    IO.File.WriteAllLines(removedPath, removedLines.ToArray)
End Sub
If you want to display the new file or removed lines file in a Listbox before/after processing you can for example do this:
VB.NET:
Me.DisplayListBox.DataSource = IO.File.ReadAllLines("file.ini")
 
Strange results? Talk about strange code... ;)

haha, I knew the whole process could have probably been done quicker. Sadly the lecturer I have at the moment is less that helpful! Most of the UI interaction was due to me learning the code as I was writing it so wanted to make sure it was done correctly.

Anyways, Thank you very much!
/Taylor
 
Back
Top