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!

Replace the Text in the file

Status
Not open for further replies.

adimulam

Programmer
Feb 13, 2002
25
US
Hi All,

Is there any in VB where you can replace a piece of text in the file and savethe file without opening the file, reading it and writing it to a separate file?. I appreciate your help.
 
Clarification needed. Example will help. Is "file" an Access table?

[purple]_______________________________
[sub]Never confuse movement with action -- E. Hemingway [/sub][/purple]
 
It is a text file. The file is of 1 MB size. There is a word inside the text file that has to be replace by some other word. I do not want to open the file and read every single line, look for that WORD and replace it. If there is a way to replace that WORD without opening and closing the file, that would be great.
 
A starting point:
Const ForReading = 1, ForWriting = 2
Dim fso As Object, f As Object, p As String, b As String
p = "\path\to\textfile.ext"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso_OpenTextFile(p, ForReading)
b = Replace(f.ReadAll, "word", "some other word")
f.Close
Set f = fso_OpenTextFile(p, ForWriting)
f.Write b
f.Close
Set f = Nothing
Set fso = Nothing

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Does this not still open the file? It closes it...which certainly implies it is opened. True, technically it is opened in a particular way, but does it not in fact open it twice? Once for Reading, once again for Writing? Is this not more I/O?

The question was asking how to NOT open it.

And no, you can not change the contents of text in a file without opening it....in some way or another.

Gerry
 
Gerry, obviously my code open the file !
My reply was an answer to this specific issue:
I do not want to open the file and read every single line, look for that WORD and replace it

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
And ReadAll does not in fact read all lines? This is pure ignorance on my part. I did not know that.

Gerry
 
And ReadAll does not in fact read all lines?
Yes, but in only one go without parsing for EndOfLine markers.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I see. So it parses all text characters, but not line feeds?

Gerry
 
But does Readall work fine, it the file size is 2 MB?
 
Yes, and probably faster than other methods too, cause it doesn't have to do all the sequental reads. See for instance thread222-1106943 and thread222-493508.

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top