I am trying to use FileSystemObject methods to find a specific line in a text file, and replace a specific line in that line. I am relatively new to this, since my current code has excel to open a text file and replace what I need to replace, and then save and close it. This method is no longer an option, since having excel opens the text file for too long and holds the file.
This is how far I have come 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 for writing, but I donβt know how to find the line and replace it with the line I need to replace.
Please let me know if, in case you are ready to help / guide me in the right direction, you need additional information. I searched a lot and saw people suggest other ways to do this. I need to learn how to edit lines this way. Can someone please help me?
Thanks in advance!
-Anthony C.
source share