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")

Related posts:

  1. Search & Delete the Line for Given Search String in a text file
  2. How to Search & Replace the Line for Given Search String in a text file
  3. How to search for a string in a text file if found, display that line to the user.
  4. How would I remove the text between two given strings
  5. How to Delete a specific line from a file (note: first line = line number 0)
  6. How to add contents of text file to a list box
  7. How to Insert a string of text in a file
  8. How to I create a text file from part of another text file (for given line numbers like Line No. 2 to 7)
  9. How to print a text file with its default printer
  10. How to write text in a given text file (in three ways)

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.