How can I get a line number that contains a specified string in a text file and write a word one line below it?
Printable View
How can I get a line number that contains a specified string in a text file and write a word one line below it?
With a little finagling anything is possible.
vb Code:
Dim file = System.IO.File.ReadAllLines("Your Document") Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("Your Document", False) Dim line As Integer = Array.Find(file, Function(s) s.Contains("searchstring")) For x = 0 To line newfile.WriteLine(file(x).ToString) Next newfile.WriteLine("Your new Line") For y = (line + 1) To file.Length - 1 newfile.WriteLine(file(y).ToString) Next newfile.Close()
something like that should work
Sorry I was slightly off... instead of Array.find() it should be Array.FindIndex()...
This only uses one text file... what this does is that it ReadsAllLines from the original text and saves it to an array, it then searches through the array for the specified line you are looking for, and then it prints the array to the same document after it deletes all the contents....
I was able to get this to work perfectly...
vb Code:
Dim file = System.IO.File.ReadAllLines("C:\doc.txt") Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("C:\doc.txt", False) Dim line As Integer = Array.FindIndex(file, Function(s) s.Contains("hello")) For x = 0 To line newfile.WriteLine(file(x).ToString) Next newfile.WriteLine("This is my new line") For y = (line + 2) To file.Length - 1 newfile.WriteLine(file(y).ToString) Next newfile.Close()
I really appreciate what you're doing to help me so far but there are still minor things i'd like to address. I hope you don't mind.
1)It finds the word, goes to one line below it but if any text if present there, it just replaces it. For example this is our hypotetical text:
...
[apes]
bonobo
chimp
[wildcats]
leopar
cheetah
tiger
lion
[wildbirds]
eagle
falcon
...
VB Code:
Dim file = System.IO.File.ReadAllLines("C:\doc.txt") Dim newfile = My.Computer.FileSystem.OpenTextFileWriter("C:\doc.txt", False) Dim line As Integer = Array.FindIndex(file, Function(s) s.Contains("[wildcats]")) For x = 0 To line newfile.WriteLine(file(x).ToString) Next newfile.WriteLine("bobcat") For y = (line + 2) To file.Length - 1 newfile.WriteLine(file(y).ToString) Next newfile.Close()
End result is this: (leopar is replaced)
...
[apes]
bonobo
chimp
[wildcats]
bobcat
cheetah
tiger
lion
[wildbirds]
eagle
falcon
...
2)If the phrase i'm looking for is in the first line of the file then it doesn't write one line below it, it replaces the phrase i'm looking for with the one i write.
So far these two issues came up.
Hello,
If this is not a school project continue.
The following example reads a text file into a List(Of String), shows it in a ListBox purely for debug purposes, otherwise everything is done against the list.
User selects an item from the ListBox (using a ListBox rather than a TextBox here to control things, feel free to use a TextBox), locate it then insert the time below it.
Second operation is to save the file back to disk as in Button2.
Code:Public Class Form1
Private FileContents As New List(Of String)
Private FileName As String = IO.Path.Combine(Application.StartupPath, "TextFile1.txt")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FileContents = IO.File.ReadLines(FileName).ToList
ListBox1.DataSource = FileContents
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'
' Use a TextBox once you see this works. When using text
' in a TextBox you might consider things like casing of text.
'
Dim Position = FileContents.IndexOf(ListBox1.Text)
If Position <> -1 Then
ListBox1.DataSource = Nothing
FileContents.Insert(Position + 1, Now.ToShortTimeString)
ListBox1.DataSource = FileContents
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
IO.File.WriteAllLines(FileName, FileContents)
End Sub
End Class
Kevin is right, the more you try to solve all these things for your self the better you are going to do if this is for a class.
Go back and look through my code and figure out what is going on and when it is being printed.... Give it a good 5 minute look and you should be able to pick out where it went wrong... Dont look below until you figure it out on your own...
The first thing that happens is that it prints lines 1 to the selected line, then it prints your newline, then it goes to the selected "line + 1" and starts printing again. Because it said "line+2" it skipped the following line making it look like it was replaced.