We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,127 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Operating with specific file lines

Hello everyone.
I'm working on a program which should edit a text file by adding/deleting some specific lines.
By default this text file doesn't have these specific lines, so the program will add them.
But I also want it to replace them if they're already in the text file.
Basically this is my idea:
- Read the text file
- Search the specific lines
- If they exist then replace them
- Else add them
I pretty much know how to read a text file, how to search for specific lines, how to replace them and how to add them, but I don't know or can't figure it out how to know the "if they exist".
I need something like "If String1.Exists = True Then..." (I know it's completely wrong, but I hope it helps you to help me).

I hope I've been clear, if not tell me I'll try my best. :)

This is part of the source code of the program

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim textfile As String = "C:\test.txt"
        If System.IO.File.Exists(textfile) Then
            'Read the text file'
            Dim lines = IO.File.ReadAllLines(SettingsFile)
            Dim string1 As Integer = Array.FindIndex(lines, Function(s) s.Contains("adf100009"))
            Dim string2 As Integer = Array.FindIndex(lines, Function(s) s.Contains("adf100010"))

            'Here should be an If string1.exists = true then...'
            'Replace the lines if found'
            Dim tempList As New List(Of String)(lines)
            tempList.RemoveAt(string1)
            tempList.RemoveAt(string2)
            tempList.Insert(string1, "adf1000" & inputvalue1)
            tempList.Insert(string2, "adf1000" & inputvalue2)
            lines = tempList.ToArray
            IO.File.WriteAllLines(SettingsFile, lines)
            MsgBox("Lines replaced")
        Else
            MsgBox("Cannot find the Text File")
        End If 

It's far from being perfect and complete. Just to give you an idea on what I'm working on. :)

Thanks for the attention.

3
Contributors
4
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
Dendari
Newbie Poster
6 posts since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

If you have them in a list, you can use .Contains() without the extra Array.FindIndex() stuff.

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

I would like to know how you file looks like (phisiclly). Then tell us what to add, change, or what ever...

Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13

If you have them in a list, you can use .Contains() without the extra Array.FindIndex() stuff.

Do you mean with this code:

If tempList.Contains(string1) = True Then

or

If lines.Contains(string1) = True Then

Tried both and it doesn't work, the program just keep adding the lines.

I would like to know how you file looks like (phisiclly). Then tell us what to add, change, or what ever...

The text file I'm gonna edit is a game config file, so it contains stuff about the resolution, texture quality, shadow quality and so on...

The lines I'm gonna add/edit/remove are connection stuff, so rate, interpolation, extrapolation, things like that.

Dendari
Newbie Poster
6 posts since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Ok I resolved it. Here's the complete working source code:

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

           'Reading Text File'
            Dim textfile As String = "C:\test.txt"
            If System.IO.File.Exists(textfile) Then
                Dim lines = IO.File.ReadAllLines(textfile)
                Dim string1 As Integer = Array.FindIndex(lines, Function(s) s.Contains("adf10000"))
                Dim string2 As Integer = Array.FindIndex(lines, Function(s) s.Contains("adf20000"))
                'Writing Text File'
                  If string1 > 0 And string2 > 0 Then
                        Dim tempList As New List(Of String)(lines)
                        tempList.RemoveAt(string1)
                        tempList.RemoveAt(string2 - 1)
                        tempList.Insert(string1, "adf10000" & TextBox1.Text)
                        tempList.Insert(string2, "adf20000" & TextBox2.Text)
                        lines = tempList.ToArray
                        IO.File.WriteAllLines(textfile, lines)
                        MsgBox("Done")
                   Else
                        Dim test As New IO.FileStream(textfile, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.None)
                        Dim myFileWriter As New System.IO.StreamWriter(test)
                        myFileWriter.WriteLine("adf10000" & TextBox1.Text)
                        myFileWriter.WriteLine("adf20000" & TextBox2.Text)
                        myFileWriter.Close()
                        test.Close()
                        MsgBox("Done")
                    End If
            Else
                MsgBox("Cannot find the textfile!")
            End If
        End Sub
Dendari
Newbie Poster
6 posts since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by thines01 and Mitja Bonca

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0671 seconds using 2.67MB