Solved

Visual Basic Text File Compare

Posted on 2011-09-27
16
335 Views
Last Modified: 2012-05-12
Greetings,

 I would love some help with having my program check two text files and finding if a value is false to create a new text file with the false value. For the example I just put some values, but the real files have about 100 lines of different numbers. Just need to extract the values that are not true. Thanks

Eample:

Text1.text;

11111111
11111111
11111111
55525512

Text2.text

11111111
11111100
11111111
55525512

Text3.text
11111100
55525512
0
Comment
Question by:Jack_Jones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36713936
what does true false mean?
what does a value mean? a line?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36713973
The two text files,

Text1.text, holds values that should be in the listview.
Text2.text, holds exported data from a listview item.
Text3.text, will receive data of the missing numbers.

My goal is to take Text3.text and have it imported to a listview to generate it's data of missing numbers. Hope this helps,

0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36713975
So,

Text1.text read and compare with Text2.text and if a value is not found to be a match, it will place it into Text3.text.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36714049
have a look at here

http://web.synametrics.com/plaintextfiles.htm

at the end you can write a query to get the difference which you can dump into another text file...
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36714068
also look at here

http://users.drew.edu/skass/sql/TextDriver.htm

you create a linked server to mssql server, then use each file as table

select a.*
from test1 a left join test2 b on A.col1=B.col1 and A.col2=B.col2
where B.col1 is null

to get the difference
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36714082
Can VB do this with a file on the hard drive, i don't have access to SQL stuff, and this program will be used on my labtop that will not have internet access.
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36714201
without sql, you can stil use ms text driver, create a user dsn, text driver, point to the folder
and use sql queries to select data from text files (each file will be a table)

instead of opening files, reading line by line, creating an array, then looping other, and checking each line in the array, and writing the difference to a text file is another method... but long way to go...
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36714216
Let me ask this, as I can get it to work. Found this using google and trying / trying harder lol.

The only part I can't seem to figure out is the part where if it's not the same to put the items from the array that are not the same into foundskus.items.add("")


 Dim LineOfText1 As String, LineOfText2 As String
        Dim i As Integer, c As Integer
        Dim arryTextFile1() As String, arryTextFile2() As String

        LineOfText1 = TextBox2.Text
        arryTextFile1 = LineOfText1.Split(",")

        For i = 0 To UBound(arryTextFile1)
            'MsgBox(arryTextFile1(i))
        Next i

        LineOfText2 = TextBox3.Text
        arryTextFile2 = LineOfText2.Split(",")

        For c = 0 To UBound(arryTextFile2)
            'MsgBox(arryTextFile2(c))
        Next c

        If arryTextFile1.Equals(arryTextFile2) Then
            'MessageBox.Show("files are the same")
        Else
            foundskus.Items.Add("Missing Items")
        End If

Open in new window

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36714231
this one does not read from files
a string delimited with ","
foundskus is an object (maybe a listbox, combo, table etc)
it does not write to file...

if you go this way, steps are:

1 open input file 1
2 reading line by line,
3 creating an array, put each line into array
4 close file
5 then open other file , write a new file, output
6 looping other,
7 checking each line in the array
8 writing the difference to a output file
9 close all files
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36714272
could you help me with that please, spent all night trying to figure it out.
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36717033
is your input files have same number of lines? and do we match same lines?
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36717108
something like these
Dim fn1 As String = "C:\Users\Owner\Documents\test1.txt"
Dim fn2 As String = "C:\Users\Owner\Documents\test2.txt"
Dim fn3 As String = "C:\Users\Owner\Documents\test3.txt"

Dim Line1, Line2 As String

Dim objReader1 As New System.IO.StreamReader(fn1)
Dim objReader2 As New System.IO.StreamReader(fn2)
Dim objWriter As New System.IO.StreamWriter(fn3)

Do While (objReader1.Peek() <> -1) and (objReader2.Peek() <> -1)
  Line1 = objReader1.ReadLine()
  Line2 = objReader2.ReadLine()
  if (Line1 <> Line2) objWriter.Write(Line1)
Loop

objWriter.Close()

Open in new window

0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 36717114
have a look at these sources...

Reading a Text File Line by Line : http://homeandlearn.co.uk/NET/nets8p3.html
How to Write to a Text File in VB .NET : http://homeandlearn.co.uk/NET/nets8p4.html
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36796521
Where would I put the End If, after the IF statement.
0
 
LVL 51

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 500 total points
ID: 36815667
i guess no need for end if (also I forgot "Then")

it is one line one statement but if you need

if (Line1 <> Line2) objWriter.Write(Line1)
-->

if (Line1 <> Line2) Then objWriter.Write(Line1)

or this one if you have multiple statements in if...then

if (Line1 <> Line2) Then
  objWriter.Write(Line1)
End if
0
 
LVL 1

Author Closing Comment

by:Jack_Jones
ID: 36816316
Worked like a charm, thank you so much!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question