3 Replies - 3594 Views - Last Post: 21 July 2009 - 01:24 AM Rate Topic: -----

#1 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

compare values from two text file.

Posted 20 July 2009 - 11:40 PM

I have two text files which contains are like this:
This is content from "C:\original.txt"

T225
T226
T229
T230
T232
T234
T236
T240

This is the some content from "C:\Drill.txt"

T230C2.4F017S3H2000
T229C3.3F005S16H2000
T226C3.3F005S16H2000
T225C3.3F005S16H2000
T224C3.3F005S16H2000
T222C0.85F034S58H1000
T219C1.1F03S5H2000

My program should work like this:
When the original files find the same values in drill file than it must overwrite the original file the line from drill file. Let say original file contain T230 than it must serch for same value in drill file if it find than it must overwrite the original file line with drill file line. The content of drill file is not fix. It will allways be update according to need.

is that posible to connect two text file?

Is This A Good Question/Topic? 0
  • +

Replies To: compare values from two text file.

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: compare values from two text file.

Posted 21 July 2009 - 12:14 AM

Create nested loops like this:

(not working code but should give you an idea)
while original.ReadLine()
  while drill.ReadLine()
	if drill.line.contains(original.line)
	  original.line = drill.line
	end if
  End while
End while



first loop goes through original lines and the loop inside it goes through drill lines and checks if the text in original line is found inside drill line. And if it is, copy it to original.
Was This Post Helpful? 0
  • +
  • -

#3 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: compare values from two text file.

Posted 21 July 2009 - 12:59 AM

View Postjanne_panne, on 20 Jul, 2009 - 11:14 PM, said:

Create nested loops like this:

(not working code but should give you an idea)
while original.ReadLine()
  while drill.ReadLine()
	if drill.line.contains(original.line)
	  original.line = drill.line
	end if
  End while
End while



first loop goes through original lines and the loop inside it goes through drill lines and checks if the text in original line is found inside drill line. And if it is, copy it to original.




I use your idea and came up with this code
 Dim tfLines As String() = IO.File.ReadAllLines("C:\kaya.txt")
		Dim objwriter As StreamWriter = File.CreateText("C:\kaya.txt")
		Dim original As String() = IO.File.ReadAllLines("C:\Drill Layer Header original.txt")


		For Each line1 As String In tfLines


			For Each line As String In original
				Dim Istart As Integer = line.IndexOf("T")
				Dim Iend As Integer = line.IndexOf("C", Istart)
				Dim strval As String = line.Substring(Istart, Iend - Istart)


				If tfLines(line1).Contains(strval) Then
					Dim val As String = line1.Replace(line1, line)
					objwriter.WriteLine(val)
				End If


			Next



		Next


This code give me error saying "Conversion from string "T15" to type 'Integer' is not valid." at this line :
If tfLines(line1).Contains(strval) Then
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: compare values from two text file.

Posted 21 July 2009 - 01:24 AM

This should work:

		Dim tfLines As String() = IO.File.ReadAllLines("C:\temp\kaya.txt")
		Dim objwriter As StreamWriter = File.CreateText("C:\temp\kaya_new.txt")
		Dim original As String() = IO.File.ReadAllLines("C:\temp\Drill.txt")


		For Each line1 As String In tfLines

			For Each line As String In original
				Dim Istart As Integer = line.IndexOf("T")
				Dim Iend As Integer = line.IndexOf("C", Istart)
				Dim strval As String = line.Substring(Istart, Iend - Istart)


				If line1.Contains(strval) Then
					Dim val As String = line
					objwriter.WriteLine(val)
				End If

			Next
		Next
		objwriter.Flush()
		objwriter.Close()



Just a small fix to if statement.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1