|Title||Remove lines containing a target string from a file|
|Keywords||delete, remove, text file, line|
|Categories||Files and Directories, Utilities|
Open the file and use Line Input to read its lines. Write any that do not contain the target into a new file.
' Remove the target lines from from_name and
' save the result in to_name. Return the
' number of lines deleted.
Private Function DeleteLines(ByVal from_name As String, _
ByVal to_name As String, ByVal target As String) As Long
Dim strlen As Integer
Dim from_file As Integer
Dim to_file As Integer
Dim one_line As String
Dim deleted As Integer
' Open the input file.
from_file = FreeFile
Open from_name For Input As from_file
' Open the output file.
to_file = FreeFile
Open to_name For Output As to_file
' Copy the file skipping lines containing the
deleted = 0
Do While Not EOF(from_file)
Line Input #from_file, one_line
If InStr(one_line, target) = 0 Then
Print #to_file, one_line
deleted = deleted + 1
' Close the files.
DeleteLines = deleted