CodeItBetter Programming Another VB Programming Blog

Search & Delete the Line for Given Search String in a text file

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'Text File Handling - Search & Delete the Line for Given Search String in a text file
Public Sub DeleteLineForGivenSearchString(ByVal strFileName As String, _
    ByVal strSearchString As String)
    'Search for text if found Delete Line
    Dim strTxt As String
    Dim strLines() As String
    Dim I As Integer
 
    'strFileName = "C:\Temp\ReadMe.txt"
    'strSearchString = "Windows contains"

    'Open the text file
    Open strFileName For Input As #1
    'load the text file in strTxt variable
    strTxt = Input(LOF(1), 1)
    'close the text file
    Close #1
    'Split the text file
    strLines() = Split(strTxt, vbCrLf)
 
    'Clear existing text
    strTxt = ""
 
    For I = LBound(strLines) To UBound(strLines)
        'search the string
        If InStr(1, strLines(I), strSearchString) = 0 Then
            strTxt = strTxt & vbCrLf & strLines(I)
        End If
    Next I
 
    'open the text file to write the content of strTxt
    Open strFileName For Output As #1
    Print #1, strTxt
    'Close the text file
    Close #1
End Sub
'
'Call this function as:
'Call DeleteLineForGivenSearchString("C:\Temp\ReadMe.txt", "This sample contains a co")
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.