Find and replace in text file error

finding and replacing in a text file

Page 1 of 1

2 Replies - 22226 Views - Last Post: 30 September 2010 - 06:28 AM Rate Topic: -----

#1 Beng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 01-January 07

Find and replace in text file error

Posted 01 January 2007 - 10:27 PM

Hey here's my problem,

I have a project to complete and i have begun the code. What i want to do is go through the text file and find evey | (pipe) symbol and change it to a , (comma). I have the code and worked out how to do it but the code will only work for the first line. I was just wondering whether any of you had any advice on the way through this problem because i am stumped.

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

		Dim strFile As String
		Dim strDocument As String

		strFile = "C:\Documents and Settings\Orbops\Desktop\Ben's stuff\1.txt"
		FileOpen(1, strFile, OpenMode.Input, OpenAccess.Read, OpenShare.LockWrite)

		FileSystem.Input(1, strDocument)

		FileClose(1)

		strDocument = Replace(strDocument, "|", ",", , , CompareMethod.Text)

		'Opens our document for output, with write only access, and locks the
		'writability until we're done

		FileOpen(1, strFile, OpenMode.Output, OpenAccess.Write, OpenShare.LockWrite)

		'Writes the strDocument text to the file

		FileSystem.Write(1, strDocument)

		'Closes the handle to the file, allowing all programs to edit the file

		FileClose(1)
	End Sub
   


Thanks in advance....!

Is This A Good Question/Topic? 0
  • +

Replies To: Find and replace in text file error

#2 psykoprogrammer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-October 06

Re: Find and replace in text file error

Posted 03 January 2007 - 09:29 AM

string strFile = @"C:\Documents and Settings\Orbops\Desktop\Ben's stuff\1.txt";
string result;

TextReader reader = File.OpenText(strFile);
result = Regex.Replace(reader.ReadToEnd(), "\\|", ",");

reader.Close();



That's in C#, but I think it should be easy to port to VB.NET.

This post has been edited by psykoprogrammer: 03 January 2007 - 09:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 cshivers  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 30-September 10

Re: Find and replace in text file error

Posted 30 September 2010 - 06:28 AM

Perhaps try this:

strDocument = strdocument.Replace("|", ",")


instead of this

strDocument = Replace(strDocument, "|", ",", , , CompareMethod.Text)

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1