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

replacing part of a string in a txt file

Status
Not open for further replies.

bkwyrm13

Programmer
Jun 22, 2005
3
US
I have a text file that I want to be able to search for a paticular string and then replace part of that string with something different.

for instance in the middle of a text file there would be this line
set gameLogic.enableVoHelp from 1 to 0

and I want to change it to this
set gameLogic.enableVoHelp from 15 to 12


any help would be appreciated
 
Welcome to Tek-Tips. To get the best from these forums please read faq222-2244 carefully. You will then find that the search function will find that this has been answered several times, and that there is a faq on this exact subject

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Ah, yes, now I feel stupid. I've surfed bout 60 pages or so back in the forum and looked over the faqs, but one search with the word replace pretty much answers my question.
Just one more question though. Is the replace command for the .net framework will it work in vb6 too (it pops up under the msdn library under the .net framework)

Sorry bout that
 
Replace works fine in VB6, but I recall not in VB5. Try to get a copy of MSDN from around mid 2001, when all the VB6 stuff was still in there. Much VB6 stuff was replaced with VB.NET around that time.

Note that many of the old VB6 functions are still there in VBA (under Word for instance at least up to Word XP) and you can often get to function help by that route (including Replace function).

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
K, I think this should do it. I was using a 2 instead of an 8 in .OpenTextFile so I haven't been able to test all of it. Or am I missing anything else?
Thanks for all the help, I've learned more from this forum than I did in the class where I first learned vb6.



Dim Oneline As String, Searchfor As String
Dim filenum As Integer
Maxbots = txtMaxBots.Text
filenum = FreeFile

Open ("filepath") For Input As #filenum
With CreateObject("Scripting.FileSystemObject")
Do While Not EOF(filenum)
Line Input #filenum, Oneline
If Left$(Oneline, 22) = "string to match" Then
Oneline = Replace(Oneline, "32", Maxbots)
End If
.OpenTextFile("filepath", 8, True).Write OneLine
Loop
End With
Close #1
Close filenum
 
I was using a 2 instead of an 8 in .OpenTextFile

probably you would have picked this error up quicker if you had used the symbolic constants in the OpenTextFile call.

Code:
'ForReading = 1
'ForWriting = 2
'ForAppending = 8
'Constants are defined already..
 .OpenTextFile("filepath", ForAppending, True).Write OneLine
 .OpenTextFile("filepath", 8, True).Write OneLine



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top