5 Replies - 2382 Views - Last Post: 06 April 2009 - 11:47 PM Rate Topic: -----

#1 meloco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-March 09

compare between 2 txt files and save it in new txt . P

Posted 02 April 2009 - 01:02 AM

iam newbie to this, hope your help

I have many txt files, and i have to select any txt file to search and compare match fields with file: CompareText.txt. My text file format:


20090227#2#B010110100#3787.562904#
20090227#2#B010110200#430556.987989#
20090227#2#B010110213#2146515.91#

20090227#2#B010110211#14073.69#
......


CompareText.txt

B010110200
B010110400
B010110413
B010110411
B010110213

, I can get the same result
(depend on the third column to compare), then i must save this result to new txt file . Examples, this result from 2 txt files:

20090227#2#B010110200#430556.987989#
20090227#2#B010110213#2146515.91#

help Pls urgent...thanks in advance
Best Regards

This post has been edited by meloco: 02 April 2009 - 01:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: compare between 2 txt files and save it in new txt . P

#2 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: compare between 2 txt files and save it in new txt . P

Posted 02 April 2009 - 02:01 AM

The following tutorial could of some use to you.
Tutorial:TextFieldParser Tutorial
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: compare between 2 txt files and save it in new txt . P

Posted 02 April 2009 - 08:10 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#4 meloco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-March 09

Re: compare between 2 txt files and save it in new txt . P

Posted 06 April 2009 - 08:00 PM

ok, thanks u all,
I find the solution to solve, that is: I will import all content with # delimited into a table called Import in Access and after that i will export this table into a datagridview and then i will export it into other text file, but i get errors when i import txt file into Access. This is my code:

Name of text file: BC0902281.A0131700101 -> i get id from a part of txt name : 01317001.
Content of txt file:
BG#01317001#D01#
20090228#5#D0101#1274193999025#
20090228#5#D0102#1227149540626#
EN#01317001#D01#2#
BG#01317001#A0101#

these lines contain EN and BG must not be inserted into acess

Private Function sqlInsert(ByVal id As String, ByVal name As String, ByVal info As String, ByVal qual As double, ByVal datetime As String) As String
		Dim sql As String
		sql = ""
		sql = sql & "INSERT INTO IMPORT (id, name, info, qual, datetime) VALUES("
		sql = sql & "'" & id & "' "
		sql = sql & ",'" & name & "' "
		sql = sql & ",'" & info & "' "
		sql = sql & ",'" & qual & "' "
		sql = sql & ",'" & datetime & "' "
		sql = sql & ")"
		sqlInsert = sql
	End Function

	Private Sub btnFileName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileName.Click
		Dim Conn As New System.Data.OleDb.OleDbConnection
		Dim Comm As New System.Data.OleDb.OleDbCommand
		Dim i As Integer
		Dim arrLine As String()
		Dim arrLines As String()
		Dim strFileName As String
		Dim sql As String
		Dim rsl As DialogResult

		Conn.ConnectionString = getConnectionString_MSACCESS("\\server\Data\test\SELECT_FILES\db1.mdb", "", "")
		Conn.Open()
		Comm.Connection = Conn

		With Me.OpenFileDialog1
			.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
			.Multiselect = True
			.Title = "Text files Browser"
			rsl = .ShowDialog()
			If rsl = Windows.Forms.DialogResult.OK Then
				For Each s As String In OpenFileDialog1.FileNames
					Dim fi = OpenFileDialog1.FileName
					strFileName = IO.Path.GetFileName(s)
					txtText.Text = fi
					MessageBox.Show(strFileName)

					arrLines = System.IO.File.ReadAllLines(strFileName)


					Dim id, name, info, qual, datetime As String
					id = ""
					datetime = ""

					If arrLines.Length > 0 Then
						datetime = arrLines(0).ToString()
						arrLine = arrLines(0).ToString().Split("#")
						datetime =  arrLine(1).Substring(4, arrLine(1).Length - 4)+ "/" +  arrLine(1).Substring(2, 2) + "/" + arrLine(1).Substring(0, 2)
					   id = strFileName.Substring(strFileName.Length - 14, 8)
					End If
					For i = 1 To arrLines.Length - 1
						arrLine = arrLines(i).ToString().Split("#")
						If arrLine(0) <> "EN" And arrLine.Length = 4 Then
							name = arrLine(0)
							info = arrLine(1)
							qual = arrLine(2)

							sql = sqlInsert(id, name, info, qual, datetime )		   Comm.Connection = Conn
							Comm.CommandText = sql
							Comm.ExecuteNonQuery()

						End If
					Next
				Next
				MessageBox.Show("Sucessfully!")
			End If
		End With
	End Sub


plz help me. thanks

This post has been edited by meloco: 06 April 2009 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: compare between 2 txt files and save it in new txt . P

Posted 06 April 2009 - 09:31 PM

A note, in VB.NET you no longer need to add to a string like str = str & "SomeValue, you can use the C++ syntax like str &= "SomeValue" (makes less typing for you)

Private Function sqlInsert(ByVal id As String, ByVal name As String, ByVal info As String, ByVal qual As double, ByVal datetime As String) As String
		Dim sql As String
		sql = ""
		sql &= "INSERT INTO IMPORT (id, name, info, qual, datetime) VALUES("
		sql &= "'" & id & "' "
		sql &= ",'" & name & "' "
		sql &= ",'" & info & "' "
		sql &= ",'" & qual & "' "
		sql &= ",'" & datetime & "' "
		sql &= ")"
		
		Return sql
End Function



Was This Post Helpful? 0
  • +
  • -

#6 meloco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-March 09

Re: compare between 2 txt files and save it in new txt . P

Posted 06 April 2009 - 11:47 PM

but my problem is my txt contains many "BG" and "EN" lines, its content:

BG#01505001#B0101#
20090327#2#B010110100#0#
20090327#2#B010110200#171391.384433#
20090327#2#B010110213#1046589.24#
20090327#2#B010110211#6973.69#
20090327#2#B01012011111#1581.58#
EN#01505001#B0101#43#
BG#01505001#A07#
20090227#2#A07910100#507135679694#
20090227#2#A07910113#8863883.56#


.....


I must remove BG and EN line and there are 2 datetime fields in the above examples: 20090327 -> 27/03/2009; 20090227 ->27/02/2009; name field is insert in access is: 2; info: is the third array...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1