Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Visual Basic Text File Compare

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
Jack_Jones
Asked:
Jack_Jones
  • 9
  • 7
2 Solutions
 
HainKurtSr. System AnalystCommented:
what does true false mean?
what does a value mean? a line?
0
 
Jack_JonesAuthor Commented:
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
 
Jack_JonesAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
Jack_JonesAuthor Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
Jack_JonesAuthor Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
Jack_JonesAuthor Commented:
could you help me with that please, spent all night trying to figure it out.
0
 
HainKurtSr. System AnalystCommented:
is your input files have same number of lines? and do we match same lines?
0
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
Jack_JonesAuthor Commented:
Where would I put the End If, after the IF statement.
0
 
HainKurtSr. System AnalystCommented:
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
 
Jack_JonesAuthor Commented:
Worked like a charm, thank you so much!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now