CodeItBetter Programming Another VB Programming Blog

How to Search & delete a string in text File

Posted on January 4, 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
'Text File Handling - How to Search & delete a string in text File
'Without using Regular Expressions

Option Explicit
Public Sub RemoveTextFromRTF(ByVal strFile As String, ByVal strRemoveTextPattern As String)
' Procedure : RemoveTextFromRTF
' DateTime  : 7/11/2006 04:19
' Purpose   : This procedure will read the rtf in text format and will remove document
'             generator informations from the rtf file.
'             This will remove the string specified in between { and } braces.
'
    Dim sText As String, lPos As Long, lStart As Long, lEnd As Long
    Open strFile For Input As #1
    sText = Input(LOF(1), #1)
    Close #1
    lStart = 1
    Do
        lPos = InStr(lStart, sText, strRemoveTextPattern)
        If lPos Then
            lStart = InStrRev(sText, "{", lPos)
            lEnd = InStr(lPos, sText, "}")
            If lEnd > 0 And lStart > 0 Then
                sText = Left$(sText, lStart - 1) & Mid$(sText, lEnd + 1)
            Else
                lStart = lPos + 4
            End If
        End If
    Loop Until lPos = 0
    Open strFile For Output As #1
    Print #1, sText
    Close #1
End Sub
'Call this function as:
'Call RemoveTextFromRTF("C:\12302.rtf","\generator")
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.