Filter out matching instances between files

Shims

Active member
Joined
Jun 15, 2003
Messages
39
I have made an inefficient filtering program that someone may be able to help me with. I am using lines from "FILE A" (aka "A") to match against lines that appear in "FILE B" (aka "B"). I would like to remove this information from "B" as soon as the match is made.

As it stands now, the program loops through "B" for every line in "A" trying to find a match. If a match is made, then the data is stored in a QUEUE. After the loop has cycled through all the lines in "A", all the data from "B" is stored in an ARRAY.

Finally, another loop is made to cycle through the ARRAY for every item in the QUEUE trying to make a match. Where the line matches, the current element in the ARRAY is set to nothing. With this loop ending, I print the every element of the ARRAY (minus the blank ones) to a file.

I realize this may be a little hard to understand, but it may help with the following code. Also, the variables might be confusing for i did not spend time trying to name them :

Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim count As Integer
        Dim count2 As Integer
        Dim count3 As Integer
        Dim int As Integer = qCreatedFiles.Count - 1
        Dim strFileDataBase As String
        Dim strDBCommand As String
        Dim strFileTitle As String
        ProgressBar1.Visible = True
        ProgressBar1.Minimum = 1
        ProgressBar1.Value = 1
        ProgressBar1.Step = 1

        For count2 = 0 To int
            strFileTitle = qCreatedFiles.Dequeue
            strDBCommand = "SELECT * From " & strFileTitle
            cmd = New OleDbCommand(strDBCommand)
            objCmd.SelectCommand = cmd
            objCmd.SelectCommand.Connection = con
            objCmd.Fill(ds, strFileTitle)
            ProgressBar1.Maximum = ds.Tables(strFileTitle).Rows.Count

            For count = 0 To ds.Tables(strFileTitle).Rows.Count - 1
                strFileDataBase = ds.Tables(strFileTitle).Rows(count)(0)
                FileScan(strFileDataBase, strFileTitle)
                ProgressBar1.PerformStep()
            Next

            ProgressBar1.Value = 1
            ds.Tables(strFileTitle).Clear()
            Print_to_File(strFileTitle)
        Next
    End Sub

Sub FileScan(ByVal str As String, ByVal strFileTitle As String)
        Dim count As Integer
        Dim count2 As Integer
        Dim intBuf As Int16 = FreeFile()
        Dim strFileData As String
        FileOpen(intBuf, "C:\Documents and Settings\Owner\Desktop\Docs\" & strFileTitle & ".txt", OpenMode.Input)

        Do Until EOF(intBuf)
            strFileData = LineInput(intBuf)
            If str = strFileData Then
                qMatchedString.Enqueue(str)
                FileClose(intBuf)
                Return
            End If
            If strFileData = "" Then
                FileClose(intBuf)
                Return
            End If
        Loop
        FileClose(intBuf)
    End Sub
 
Last edited by a moderator:
youd be much better off using streamreader / writer.
eg:
Code:
Imports System.IO
/// at top of your code window^^^^
///
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Thefile As String = "C:\test.txt"
        FileScan("", Thefile)
    End Sub

    Sub FileScan(ByVal str As String, ByVal strFileTitle As String)
        Dim strReader As StreamReader = New StreamReader(New FileStream(strFileTitle, FileMode.Open))
        str = strReader.ReadLine
        While strReader.Peek <> -1
            /// your sorting code here
            TextBox1.AppendText(str)
            str = strReader.ReadLine
        End While
        strReader.Close()
    End Sub
putting something like your lines text from the database where it says "FileScan("", Thefile)" and then you could do an if else , like this :
Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Thefile As String = "C:\test.txt"
        Dim TheLine As String = "testing 123" /// your text to compare here.
        FileScan(TheLine, Thefile)
    End Sub

    Sub FileScan(ByVal str As String, ByVal strFileTitle As String)
        Dim strReader As StreamReader = New StreamReader(New FileStream(strFileTitle, FileMode.Open))
        If str = strReader.ReadLine Then
            MsgBox("you already have that line!")
            str = ""
        Else
            str = strReader.ReadLine
        End If

        While strReader.Peek <> -1
            TextBox1.AppendText(str)
            str = strReader.ReadLine
        End While
        strReader.Close()
    End Sub
 
Can i write to the file while it is in open mode?

So as soon as the match is made i can just write to the file?
I dont see how this is done in the code. i am not familiar with the stream writer.
 
Back
Top