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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Challenge with Replace Method

Status
Not open for further replies.

incron

Technical User
Mar 16, 2002
60
US
Hey Guys

Im trying to replace some text in a file. But when I use the Replace method it deletes a large portion of the file even though Im not using the Start argument here is the code

dim fso, gf, f, rtf, gtf

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso_OpenTextFile("strngtext.txt", 1)
rtf = f.ReadAll
f.Close


Set gf = fso_OpenTextFile("MultiUseDev.txt", 1)
gtf = gf.ReadLine
gf.Close


On Error Resume Next

Set NewRepos = ""
dstrContents = Replace(gtf,rtf,NewRepos)


Set xf = fso_OpenTextFile("MultiUseDev.txt", 2)
xf.Write dstrContents
xf.Close
 
[0] You use on error resume next just to avoid seeing your wrong line of setting NewRepos to empty string? Why not better spend your time on the basic saving you time in the long run?
[tt]
[red]'[/red]On Error Resume Next
[red]'[/red]Set NewRepos = ""
NewRepos=""
[/tt]
[1] Then which file and what to replace what? MultiUseDev.txt you read one line (ReadLine) and save to it overwritting. Those lines from the 2nd onward ("a large portion") would be lost, that's logic. If you want eliminate the portion of it coinciding with stringtext.txt, you read it all.
[tt]
Set gf = fso_OpenTextFile("MultiUseDev.txt", 1)
gtf = gf.Read[red]All[/red]
gf.Close

NewRepos = ""
dstrContents = Replace(gtf,rtf,NewRepos)
[/tt]
 
tsuji - I put the wrong code up Sorry, I did use the Read All method. The results were the same it replaced the text but deleted a large portion of text prior to the text that was replaced.
 
Can you post the actual code? Or did you get it fixed?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top