Results 1 to 2 of 2

Thread: Searching for a string in a text file line by line

  1. #1
    Megatron
    Guest
    eiSecure: The following code will work with single instances of the ~BEGIN and ~END tags, but it doesn't work with multiple instances (but you should be able to add it in if you play around with it for a bit).
    VB Code:
    1. Dim sFile As String
    2. Dim saFile() As String
    3. Dim strText As String
    4.  
    5. Open "MyFile.txt" For Input As #1
    6. sFile = Input(LOF(1), 1)
    7. Close #1
    8.  
    9. saFile = Split(sFile, vbCrLf)
    10.  
    11. For y = 0 To UBound(saFile)
    12.     If Trim$(saFile(y)) = "~BEGIN" Then
    13.         For x = y To UBound(saFile)
    14.             If Trim$(saFile(x)) = "~END" Then
    15.                 For Z = y + 1 To x - 1
    16.                     strText = strText & saFile(Z) & vbCrLf
    17.                 Next Z
    18.                 strText = strText & vbCrLf
    19.             End If
    20.         Next x
    21.     End If
    22. Next y
    23.  
    24. Text1 = strText

  2. #2
    DaoK
    Guest
    VB Code:
    1. Private Sub Form_Load()
    2. Dim theString As String
    3. Dim startCount As Integer
    4. Dim endCount As Integer
    5. Dim block1 As String
    6. Dim block2 As String
    7. Dim i As Integer
    8.  
    9. block1 = "[Start]"
    10. block2 = "[End]"
    11. theString = block1 & vbCrLf & "asdf" & vbCrLf & block2 & vbCrLf & _
    12. block1 & vbCrLf & "jjjp" & vbCrLf & block2
    13.  
    14. startCount = 1
    15. For startCount = 1 To Len(theString)
    16. startCount = InStr(startCount, theString, "[Start]", vbTextCompare)
    17. If startCount = 0 Then Exit For
    18. endCount = InStr(startCount, theString, "[End]", vbTextCompare)
    19. Debug.Print Mid(theString, startCount + Len(block1), endCount - (startCount + Len(block1)))
    20. startCount = endCount
    21. Next startCount
    22.  
    23. End Sub

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