|
-
May 15th, 2013, 08:59 AM
#1
Thread Starter
New Member
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
-
May 15th, 2013, 09:35 AM
#2
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.
-
May 15th, 2013, 09:50 AM
#3
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
-
May 15th, 2013, 11:16 AM
#4
Thread Starter
New Member
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.
-
May 15th, 2013, 01:01 PM
#5
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.
-
May 16th, 2013, 03:26 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|