Hi. I need to remove the lines of a text file if it contains a certain word. For example, if the line contains the word "apples", then I want that line deleted.
The below code only copies the unwanted lines to another text file. But I want to completely remove the lines. Either that, or copy everything else over, WITHOUT the lines containing "apples". How do I modify the code below to accomplish this? Thanks.
The below code only copies the unwanted lines to another text file. But I want to completely remove the lines. Either that, or copy everything else over, WITHOUT the lines containing "apples". How do I modify the code below to accomplish this? Thanks.
Code:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Input.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine
intFailure = InStr(strLine, "apples")
If intFailure > 0 Then
strNewText = strNewText & strLine & vbCrLf
End If
Loop
objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("C:\Output.txt", ForWriting, True)
objTextFile.Write(strNewText)
objTextFile.Close