search and replace text in a .txt file

xelior

Member
Joined
Aug 15, 2006
Messages
8
Programming Experience
Beginner
Hello,

I am new with VB.NET.
I want to search and replace some text in a .txt file.
For example:

In the textfile is the word #name#, this must be replaced by the name typed in a textbox in VB.NET.

I figured out how to put the text in a textfile but then it first cleans the whole file and places the text in the first line.

Can someone please help me?

Tnx
 
VB.NET:
Dim filePath As String = "file path here"
Dim contents As String = IO.File.ReadAllText(filePath)

contents = contents.Replace("#name#", myTextBox.Text)
IO.File.WriteAllText(filePath, contents)
 
Last edited:
error: ReadAllText is not a member of System.IO.File

Thanks for the quick reply, but when I use your script I get the following error :

ReadAllText is not a member of System.IO.File

Is this because I use 2002?

I hope to hear from you soon.

Tnx
 
If your primary platform is not 2005 then please change your profile to reflect that. If it is but a particular question relates to a different version then you should specify that. Noone likes wasting their time. This will work in all versions:
VB.NET:
Dim filePath As String = "file path here"
Dim reader As New IO.StreamReader(filePath)
Dim contents As String = reader.ReadToEnd()

reader.Close()
contents = contents.Replace("#name#", myTextBox.Text)

Dim writer As New IO.StreamWriter(filePath)

writer.Write(contents)
writer.Close()
 
I am sorry

Thxns fot the quick replay again.

I am sorry that I did'nt changed my platform in my profile.
I have changed it now.

Sorry
 
display and change text in text file

Hello,

I have asked how to change a word in a textfile.
This was possible and works very nice.

But now I have with the following problem:

In my textfile (unattended.txt) I have a line:
ProductID=XXXXX-XXXXX-XXXXX-XXXXX-XXXXX

I tried to show this number in a (rich)textbox and than change it and save it but I can't get it right.
Is it possible to search for this line and show only the XXXXX-XXXXX-XXXXX-XXXXX-XXXXX and than change this and save it.
And if I run the application again, it shows the changed characters, so I can change it again?

Tnx
 
Have a check into pattern matching with regular expressions. You've already learned how to parse a textfile with the IO class so now you just need to find the bit of text you want. As a step in right direction here's a regex that will match the pattern you have stated above.


VB.NET:
\b\d{5}-\d{5}-\d{5}-\d{5}-\d{5}


The above regex will when used parsing a textfile will find a pattern that exactly matches. Once you found it, just a matter of loading it into your app, change it as needed. Find the position again then use a replace to replace with what was already there.

 
Ok, we can take this a step at a time. I read in a previous post that you know how to use the stream reader/writer classes in the System.IO namespace? If thats so, then make a start by reading the textfile into a string.

VB.NET:
Dim oFile as System.IO.File
Dim oRead as System.IO.StreamReader
oRead = oFile.OpenText(text file path)
Dim EntireFile as String
EntireFile = oRead.ReadToEnd()

My code is assuming that the text file isn't too large? Get that bit working and we'll move onto the next
 
thanks

I understand the code you just gaven me, but where do I place it?
I now placed it under a button klick, and it works but where does it need to be placed when I want to load if when the program runs?

The textfile is not large, it's a unattended.txt for unattended install of WinXP.

I hope to hear from you soon.
 
Putted in load

Thanks,

I putted it in load and tested it with a messagebox.show(
EntireFile). And it shows the entire file correctly.

I will be oline again tomorrow.

Thanks so far.


 
Hi xelior, sorry really busy i'll have that demo for you later on today. In the meantime try a few things yourself. You never know you might get it cracked. Look in to.

VB.NET:
String.Replace
VB.NET:
Regex or regular expressions
 
Back
Top