Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using the Replace function on a file with many lines of text

Status
Not open for further replies.

CWebster

Programmer
Jun 12, 2002
38
GB
I can successfully use the Replace function when there's only one line of text. However, using it over a large document only ever returns the top line, and only the top line.

For example, take this:
________________
Hi there
My name is Colin
________________

When I replace "Colin" with "Fred", all I get is:
________________
Hi there
________________


Any ideas?
 
Can you post the code - I'm not convinced that the problem is in the Replace function.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
I've just checked something - it's to do with the way that I load the text file into my program. I use:

Input #1, File

What do I use to get the whole file?

I guess I could Line Input everything into an array then check each line of the array....is there a better way?
 

Dim InFile as Integer
Dim OutFile as Integer
Dim InString as String
Dim OutString as String

InFile = FreeFile
Open <Input File> for input as #InFile
OutFile = FreeFile
Open <Output File> for output as #InFile

Do While EOF(InFile) = False
Line Input #InFile, InString
OutString = Replace(InString, &quot; Colin &quot;, &quot; Fred &quot;)
Print #OutFile, OutString
Loop

Close #InFile
Close #OutFile
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
That looks like it should work Cajun, but only one question: I'm writing TO and FROM the same file - is it still possible the way that you write it?

Not having tried it, and being a novice, I reckon that it wouldn't as we're asking VB to open it for input and output at the same time. Is this where Random comes into play?
 
If you're dealing with standard text files, then no, you cannot read and write to the same file. But you can do the process as shown above, then do the following:

Kill <input filename>
Name <output file> as <input file>
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Thanks for your help, I'll go give it a try.

It's actually a HTML file that I will want to be changing eventually, if that makes a difference.

Colin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top