Results 1 to 3 of 3

Thread: Deleting Line from a text file???

  1. #1

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Arrow

    How do I search a first few characters of a line in a text file and if the characters match the given criteria, delete the entire line from the file.

    Any help will be appreciated.

    Thanks


    Kinjal

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
    Option Compare Text
     
    
    Private Sub Form_Load()
        Dim myArr() As Variant, myLine As String
        Dim i As Integer, intNum As Integer
        
        intNum = FreeFile
    '
    'open file
        Open "C:\my documents\myfile.txt" For Input As intNum
        
       Do While Not EOF(intNum)
            i = i + 1
            ReDim Preserve myArr(1 To i) As Variant
            Line Input #intNum, myLine
            If myLine Like "*character*" Then
                i = i - 1
                Else
            myArr(i) = myLine
            
        myArr(i) = Trim(myArr(i))
    End If
            
        Loop
            Close #intNum
    
     'open file
        Open "c:\my documents\myfile.txt" For Output As intNum
            For i = 1 To UBound(myArr)
                Print #intNum, myArr(i)
    
    'list box is optional just for display
              '  List1.AddItem myArr(i)
            Next
        Close #intNum
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    The following might help, but I haven¡¯t tried it. There may be mistakes. Let¡¯s wait for others¡¯ comments.

    Code:
    -----------------------------------------
    Option Explicit

    Dim strTextFile As String
    Dim strCharacters As String
    Dim strText As String
    Dim strTextLine As String
    Dim intFileNo As Integer

    Private Sub Form_Load()

    strTextFile = ¡°MyFile.txt¡±
    strCharacters = ¡°MyCharactersToFind¡±

    intFileNo = FreeFile
    Open strTextFile For Input As #intFileNo
    Do Until Eof(intFileNo)
    Line Input #intFileNo, strTextLine
    If Left$(strTextLine, Len(strCharacters)) <> strCharacters Then
    strText = strText & strTextLine & Chr$(13) & Chr$(10)
    End If
    Loop
    Close intFileNo

    intFileNo = FreeFile
    'This will overwrite the original text file. If the original is to be preserved, use another file name.
    Open strTextFile For Output As #intFileNo
    Print #intFileNo, strText
    Close intFileNo

    End Sub
    --------------------------------------
    Visual Basic Professional 6.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width