searching a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manontheedge
    New Member
    • Oct 2006
    • 175

    searching a text file

    Code:
    Private Sub readFile()
        
        Open "example.txt" For Input As #1
    
        Dim sInputData As String
        
        While Not EOF(1)
            
            Line Input #1, sInputData   
    
            If InStr(1, sInputData, "word") Then
    
                tbDataIn = tbDataIn & vbCrLf & sInputData
    
            End If
        
        Wend
        
        Close #1
    this is how I'm reading a file basically. I'm curious if it's possible to search a file with text such as

    ___AAA
    111
    222
    333
    ___BBB
    333
    222
    ___CCC
    555

    (the underscores are there to show a space)
    ...and what I want to do, is read that data in, and search for "AAA" for example (which isn't a problem), but when I find what i'm searching for, I want to pull the next lines out with it UNTIL i get to another line that starts with a space. So, if i search for "AAA", the result would be...

    AAA
    111
    222
    333
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Just set a flag when you find your starting line (AAA), then clear the flag or exit your loop when you see the ending line. Something like...
    Code:
    Start Loop
      Get line from file
      If flag is set Then
        If Line starts with space Then
          Clear Flag (or exit loop)
        Else
          Process this line
        End If
      Else
        If AAA Then
          Set Flag
        End If
      end If
    Disclaimer: The above is not valid Visual Basic syntax. :)

    Comment

    • ChillUmesh
      New Member
      • Feb 2007
      • 20

      #3
      after u got the searched string then
      code:
      do
      read next line
      loop until (mid(line,1,1)= " ") ' u can read till another line starts with " "

      Comment

      Working...