tags:

views:

841

answers:

5

Following hot on the heels of my question about how to read from a line in vb .net, I need to do the following:

change the line in a text file

[Path] = "c:\this\certain\path\"

with this line

[Path] = "c:\that\other\newer\path\"

these paths will most certainly be different lengths, so I need to either replace what's in the quotes or erase the line completely and enter a new one, but in the same spot, not appended to the end of the document.

Any help would be appreciated. Thanks!

--Adam

A: 

Hi, read the text file into a string, iterate over each line and check if it's in format [Path] = "...." (with regular expressions or simply with string.StartsWith("[Path] = ") )

In this loop you should be writing out all other lines and when you are on this [Path] line, print out the modified one.

So in code (sorry, c#):

var reader = File.OpenText("foo.txt"); 
var writer = new StreamWriter("output.txt");
string line;
while((line=reader.ReadLine()) != null)
{
   if(line.StartsWith("[Path]"))
     writer.WriteLine("[Path] = \"c:\\that\\other\\newer\\path\\\"");
  else
     writer.WriteLine(line);
}

of course Close and Dispose the StreamReader and StreamWriter

Axarydax
can I just use streamwriter to output to the same text file, or are you saying to re-write every line to the new file then rename it back to the old?
Niphoet
Try not to write in the same file. It's much safer that way! You lose almost nothing by creating a new file and overwriting the old one.
alex
You can add File.Copy("output.txt","foo.txt",true); File.Delete("output.txt"); so it'd appear that the original file was overwritten.
Axarydax
+1  A: 

Here's the deal: due to the way files are stored on disk, you can't write to one line without also updating every line the follows it. There are number of ways to do this, and the one most appropriate for your situation will depend on things like the size of the file, are you doing this to a lot of files, where in the file you expect to find this, etc. But most of the time what I like to do is actually create a copy of the old file... so as I seek through the file looking for the line(s) I need to change, I'm also writing what I've read to a new location. When I find the line, I write out the new information. I then keep seeking through the file until I reach the end at which time I close both streams, delete the original, and rename the new one.

Joel Coehoorn
A: 

One quick way is to use readAllLines and WriteAllLines:

dim ss() as string
ss = file.readallines([path])
ss(47) = "c:\that\other\newer\path\"
file.writeallines([path], ss)

If you don't know which line to change, you can search through the array ss for it.

xpda
A: 

If you really know exactly how the line you want to replace looks and the file you're reading isn't really big, you could try to just use Replace() to add the new line instead of the old one:

Dim reader As New StreamReader("foo.txt")
Dim writer As New StreamWriter("output.txt")

Dim s = reader.ReadToEnd().Replace("[Path]: C:\oldPath\file.txt", "[Path]: C:\newPath")
writer.Write(s)
alex
A: 

This will do the trick

    Dim thefile As String = "filepath"
    Dim lines() As String = System.IO.File.ReadAllLines("filepath")

    lines(number of line you want to replace) = "write what you want to replace here"

    System.IO.File.WriteAllLines(filepath, lines)
Raymond C Borges Hink