Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleRemove lines containing a target string from a file
Keywordsdelete, remove, text file, line
CategoriesFiles 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
    ' target.
    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
        Else
            deleted = deleted + 1
        End If
    Loop

    ' Close the files.
    Close from_file
    Close to_file

    DeleteLines = deleted
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated