7 Replies - 5969 Views - Last Post: 12 October 2009 - 03:07 PM Rate Topic: -----

#1 Kendall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-September 09

Deleting Duplicate lines in a Text File in vb.net using an array

Post icon  Posted 27 September 2009 - 11:43 PM

Hello,

I am adding line entries in a .ini file. The problem is, I am creating duplicate lines in the file, which I do not want to do.
I searched this issue online, and the consensus is to use an array, loop through for duplicates and create a new file.
However, being so new at this and teaching myself how to code no less, I am at a dead end.
This is not a school project, it is something I am doing for work on my own time. I have all of my code done for the program except for this last step.
Can anyone help me use an array with a text file and remove duplicates in vb.net?
Thanks
-Kendall

This is what I have for my code to enter data in the ini file. It puts two entries in one file.


Dim objReader As New StreamReader("c:\windows\odbc.ini")

		Dim line As String = ""
		Dim temp As String = ""
		Dim index As Integer = 0

		While Not objReader.EndOfStream

			line = objReader.ReadLine()

			If index = 0 Then

				temp = temp & line

			Else
				
				temp = temp & vbCrLf & line

			End If

			If line = "[ODBC 32 bit Data Sources]" Then

				temp = temp & vbCrLf & ComboBox2.Text & "=DataDirect 4.0 Oracle (32 bit)"

			End If

			index = index + 1

		End While

		temp = temp & vbCrLf & "[" & ComboBox2.Text & "]" & vbCrLf & "Driver32=C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL"

		objReader.Close()

		File.WriteAllText("c:\windows\odbc.ini", temp)

	End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Deleting Duplicate lines in a Text File in vb.net using an array

#2 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Deleting Duplicate lines in a Text File in vb.net using an array

Posted 28 September 2009 - 02:35 AM

It adds one line for me.
Then when I run it again, it adds another.
You need to test to see if the line already exists before adding it.

It's going to be simpler for you to use a third party library.

Here's one:
http://sourceforge.net/projects/nini/

It has documentation in the zip file.
Was This Post Helpful? 0
  • +
  • -

#3 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Deleting Duplicate lines in a Text File in vb.net using an array

Posted 28 September 2009 - 03:00 AM

And a quick example, as it is very easy!

nini nomenclature:
[blah] <-- this is a "config"


' Use Project-->Add Reference to add a reference to the 
' Nini\Bin\DotNet\2.0\Release\nini.dll file
Imports Nini.Config

Public Class Form1

	Private Const odbcIni As String = "C:\test\odbc.ini"
	Private Const dataSources As String = "ODBC 32 bit Data Sources"

	Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
		' Get an interface to the config source.
		' An interface defines the methods (subs and functions) that we can use.
		' "Config source" is the term because nini can do other configuration sources like xml and the registry
		Dim source As IConfigSource = New IniConfigSource(odbcIni)
		' We'd better check that the first config exists, 
		' and create it if it doesn't
		If source.Configs(dataSources) Is Nothing Then
			source.Configs.Add(dataSources)
		End If		
		' Now we want to add a key=value pair to the  config:
		' The Key:"Test" and the value "DataDirect 4.0 Oracle (32 bit)"
		' You can use your combobox text instead of "Test".
		' The dll is clever enough that it won't add it if it already exists.
		source.Configs(dataSources).Set("Test", "DataDirect 4.0 Oracle (32 bit)")

		' Finally we want a new config, called "Test", if it doesn't already exist:
		If source.Configs("Test") Is Nothing Then
			source.Configs.Add("Test")
		End If
		' And add the last key=value pair
		source.Configs("Test").Set("Driver32", "C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL")
		' And write the changes back
		source.Save()
	End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Deleting Duplicate lines in a Text File in vb.net using an array

Posted 28 September 2009 - 07:55 AM

It appears to always add an extra line after "[ODBC 32 bit Data Sources]". You need to read and toss the original line after that line so that it can be replaced correctly. Something like (untested!):
		Dim objReader As New StreamReader("c:\windows\odbc.ini")
		Dim line As String = ""
		Dim temp As String = ""

		While Not objReader.EndOfStream
			line = objReader.ReadLine()
			temp &= line & vbCrLf

			If line = "[ODBC 32 bit Data Sources]" Then
				line = objReader.ReadLine() ' read the extra line to avoid duplicating
				temp &= ComboBox2.Text & "=DataDirect 4.0 Oracle (32 bit)" & vbCrLf
			End If
		End While

		temp &= "[" & ComboBox2.Text & "]" & vbCrLf & "Driver32=C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL"
		objReader.Close()
		File.WriteAllText("c:\windows\odbc.ini", temp)
	End Sub



That should fix the problem I see... (Also index is unnecessary)
Was This Post Helpful? 0
  • +
  • -

#5 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Deleting Duplicate lines in a Text File in vb.net using an array

Posted 28 September 2009 - 09:33 AM

If we use these terms:

[Section Header1]
key1=value1
key2=value2
key3=value3
[Section Header2]
key4=value4


Then there are all sorts of states that the file could be in before the function runs. It might have the section header or not. If it has the section header, it might already have the key=value pair, or not. If it has the key=value pair, it could be on any of the lines in that section.

There are so many variables that I wouldn't want to do this with string manipulation. It's easier to just read it into a memory structure that abstracts the file structure (or to use the Windows API). As the dll does this anyway, I jumped straight to suggesting that. It takes care of all the scenarios, I think.
Was This Post Helpful? 0
  • +
  • -

#6 Kendall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-September 09

Re: Deleting Duplicate lines in a Text File in vb.net using an array

Posted 28 September 2009 - 09:08 PM

The two values to be added in the odbc.ini file and will look like the following...

[ODBC 32 bit Data Sources] <-- Under this heading (This is a constant value at the top of the file)

(1) xxxx=DataDirect 4.0 Oracle (32 bit) <-- Four digit db name, followed by "=DataDirect 4.0 Oracle (32 bit)"

(2) [xxxx] <-- Four digit db name in brackets added at the end of the file
Driver32=C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL <--Driver name added at the end of the file

___________________________________________________________

Here is a scaled down version of the odbc.ini...

[ODBC 32 bit Data Sources]
ABCD=DataDirect 4.0 Oracle (32 bit)
EFGH=DataDirect 4.0 Oracle (32 bit)
MS Access Database=Microsoft Access Driver (*.mdb) (32 bit)
Excel Files=Microsoft Excel Driver (*.xls) (32 bit)
dBASE Files=Microsoft dBase Driver (*.dbf) (32 bit)
[MS Access Database]
Driver32=C:\WINDOWS\system32\odbcjt32.dll
[Excel Files]
Driver32=C:\WINDOWS\system32\odbcjt32.dll
[dBASE Files]
Driver32=C:\WINDOWS\system32\odbcjt32.dll
[ABCD]
Driver32=C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL[
[EFGH]
Driver32=C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL


As you can see, the db name is not necessarily the first entry in the file. It can be anywhere in the file. The entries should always be in this format. I should be able to check for duplicates prior to entering data into this file, I just don't know how to do it. Do you think it will be difficult to do this without a third party solution. I am not oposed to it, I would just like to 'know' how to do it myself.

Thanks for your help.

-Kendall
Was This Post Helpful? 0
  • +
  • -

#7 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Deleting Duplicate lines in a Text File in vb.net using an array

Posted 29 September 2009 - 03:54 AM

You don't want to delete all duplicate lines:
Driver32=C:\WINDOWS\system32\odbcjt32.dll
correctly appears in many sections. You want to delete duplicate key-value pairs in each section.

You can't say that:
[ODBC 32 bit Data Sources]
Will always be the first line. There could be a comment first, or another section.

It's not just duplicates you have to worry about, there could be an existing entry under a section with the same key:
[EFGH]
Driver32=C:\Program Files(x86)\Merant\ODBC40\Drivers\IVOR817.DLL
Driver32=C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL


If it were me, and I didn't want to use the library, then I would write something that takes the same OOP approach as the library.

The procedural/string manipulation approach could use a method to ensure that a key value pair appeared under a section. You would then call it for each of the two entries:

- Pass in the file string, section header, key and value
- If the section header exists in multiple places then merge them all into one.
- Else create the section header
- Remove duplicate entries in the section.
- Remove any key=value pairs that start with the key passed in as an argument
- Add the desired key=value pair.
- return the altered string
Was This Post Helpful? 0
  • +
  • -

#8 Kendall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-September 09

Re: Deleting Duplicate lines in a Text File in vb.net using an array

Posted 12 October 2009 - 03:07 PM

Issue is resolved. Here is a resolution that works.

Dim objReader As New StreamReader("c:\windows\odbc.ini")

		Dim line As String = ""
		Dim temp As String = ""
		Dim index As Integer = 0
		Dim bUpdateFile As Boolean = True

		While Not objReader.EndOfStream

			line = objReader.ReadLine()

			If line.LastIndexOf(ComboBox2.Text) > -1 Then
				bUpdateFile = False
				Exit While
			End If
			
		End While

		objReader.Close()

		objReader = New StreamReader("c:\windows\odbc.ini")

		If bUpdateFile Then
			
			While Not objReader.EndOfStream

				line = objReader.ReadLine()

				If index = 0 Then
					temp = temp & line
				Else
					temp = temp & vbCrLf & line
				End If
				If line = "[ODBC 32 bit Data Sources]" Then
					temp = temp & vbCrLf & ComboBox2.Text & "=DataDirect 4.0 Oracle (32 bit)"
					
				End If
				
				'Index reading one line at a time
				
				index = index + 1
				
			End While
			
			objReader.Close()

			temp = temp & vbCrLf & "[" & ComboBox2.Text & "]" & vbCrLf & "Driver32=C:\Program Files\Merant\ODBC40\Drivers\IVOR817.DLL"

			File.WriteAllText("c:\windows\odbc.ini", temp)


Thanks to everyone that replied with your input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1