Results 1 to 6 of 6

Thread: Search File for String then Return Values After

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    11

    Search File for String then Return Values After

    I am fairly new to visual basic and have done some small programs in it. I am unsure whether I am needing to use a split, instr() or something totally different. I have searched and find many variations. However, after reading a ton of stuff it has added up to a bit of confusion.

    I have a file that I am needing to find a specific string in and then return the next seven numbers after that. The file has many occurrences of the string I am searching for so I will need to put the code into a loop so that it keeps finding each occurrence.

    Basically the code will search the file and find the first occurrence of this string. Then it will probably go into a select statement where a specific action will be performed. From there the code would loop around and search for the next occurrence of the string and do it all over again.

    Below is an example of the string I am searching for and how the 7 numbers afterwards are arranged. The number string might be up to 9 characters long but I only need the first 7 numbers.

    REF*EJ*5555555-5

    Any helps or guidance is greatly appreciated.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Search File for String then Return Values After

    I don't think that 'Split' will help much. InStr is probably what you need.

    Others may disagree but I'd read the entire file into a string variable, use InStr to locate the string required, process and repeat.
    Code:
    intFile = FreeFile
    Open "c:\mydir\myfile.txt" For Input as intFile
    strContents = Input(LOF(intFile), intFile)
    Close intFile
    lngStart = 1
    Do
        lngPos = InStr(lngstart, strContents, strSearch)
        If lngPos > 0 Then
            strNumber = Mid$(strcontents, lngpos + Len(strSearch), 7)
            '
            ' Do whatever with the number found
            '
            lngStart = lngpos + Len(strSearch)
        End If
    Loop Until LngStart >= Len(strContents) Or lngPos <= 0
    Where strSearch contains the String you're searching for.
    (not tested as i'm on my pocket pc)
    Last edited by Doogle; May 15th, 2013 at 11:54 AM.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Search File for String then Return Values After

    First, welcome to the Forum!
    Next...is the file expected to be a huge text file, or maybe just a coupla pages long? Could make a difference on how you could approach it. You can break the file into 'sections' using split(), putting each set of 7 numbers into the split array.

    e.g. Dim mArray() as String
    mArray = split(~your text here~,"REF*EJ*)
    REM loop through your array to get only 7 'numbers'
    Dim x as Integer
    For x = 0 to ubound(mArray)
    mArray(x) = mid(mArray(x), 1, 7)
    Next X
    REM You SHOULD have your sets of numbers in mArray (untested code)

    EDIT: (posted during Doogle's post)--I don't DISAGREE, Doogle....but I think split() would work fine - Sam

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    11

    Re: Search File for String then Return Values After

    Thanks for the welcome. I am glad I ran across this forum because it seems like a good place to learn and get help.

    The text file is a couple of pages long but it can vary on size. The code I am working on is going into a macro that will run through this file finding those numbers and then doing a specific action with that number.

    I will take a look at the codes that you guys suggested and see what works best but I am sure I can't go wrong either way. I really want to learn to be able to code as quickly as you guys did but I just don't know where to fully start. I have just been learning little by little when I get a request for something but that isn't to often.

    I do appreciate your help on this.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Search File for String then Return Values After

    If one of your files is not of sensitive nature....attach it and I'll run some examples if you get stuck.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Search File for String then Return Values After

    Yet another method, using the 'much under-used' VB6 Filter Function
    Code:
    Option Explicit
    
    Private Sub Command_Click()
    Dim intFile As Integer
    Dim intI As Integer
    Dim intPrefix As Integer
    Dim strContents() As String
    Dim strRequired() As String
    Dim strAllData As String
    Dim strSearchFor As String
    Dim strNumber As String
    strSearchFor = "REF*EJ*"
    intPrefix = Len(strSearchFor) + 1
    intFile = FreeFile
    Open "C:\myDir\MyFile.txt" For Input As intFile
    strAllData = Input(LOF(intFile), intFile)
    Close intFile
    strContents = Split(strAllData, vbNewLine)
    strRequired = Filter(strContents, strSearchFor)
    '
    ' To ignore case in the Filter operation use
    ' strRequired = Filter(strContents, strSearchFor,,vbTextCompare)
    '
    If UBound(strRequired) >= 0 Then
        For intI = 0 To UBound(strRequired)
            strNumber = Mid$(strRequired(intI), intPrefix, 7)
            '
            ' do whatever with the number
            '
            Debug.Print strNumber
        Next intI
    Else
        MsgBox strSearchFor & " Was not found in the File"
    End If
    End Sub
    Last edited by Doogle; May 16th, 2013 at 03:59 AM. Reason: added code to check if anything was found

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