I am trying to use FileSystemObject methods to find a specific line in a text file, and within that line replace a specific string. I am relatively new to this, as my current code has excel open the text file and replace what I need it to replace then save and close it. This way is no longer an option, as having excel open the text file takes too long and holds up the file.
This is how far I have gotten so far.
-
Sub FindLines()
Const ForReading = 1
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSO = FSO.OpenTextFile("C:\Users\Carella Home\Desktop\boomboom.txt", ForReading, False)
Do Until objFSO.AtEndOfStream = True
go = objFSO.ReadLine
If InStr(1, go, "ant", vbTextCompare) > 0 Then
bo = Replace(go, "t", "wow")
End If
Loop
objFSO.Close
Set objFSO = FSO.OpenTextFile("C:\Users\Carella Home\Desktop\boomboom.txt", 2)
End Sub
-
The best I can do is open the file up to write, but I have no idea how to find the line and replace it with the line that I need to replace it with.
Please let me know if, in the event that you are willing to help/guide me in the correct direction, you need more information. I have searched a lot and have seen people suggest other ways of doing this. I need to learn how to edit lines this way. Can someone please help me?
Thanks in advance!
-Anthony C.