I have a file called infile.txt where there are some datas. After so many lines again same data repeats with having different value.
I want to search value for particular parameter calledSubscriberIMSI & throughout the file where this value matches for calledSubscriberIMSI, it will take all the datas & produce it to a output file called outfile.txt.
Pls note that for each record, UMTSGSMPLMNCallDataRecord is common which i have made in bold & again starts another record. Now, I want to search data 404281130154600 for calledSubscriberIMSI . In this case it has ben found twice. so my program should take the whole segment i.e from UMTSGSMPLMNCallDataRecord to originatingAddress for 1st occurance & again from UMTSGSMPLMNCallDataRecord to originatingAddress for 2nd ocuurance & put it to a file outfile.txt.
pls help me to solve the issue..I have attached an input file infile.txt
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Thnkx westconn1 for the post.
But whenever I run it with a big input file it's showing "Subscript out of range" on the line ReDim Preserve file2(j - 1)..
What is the value of j when you get that error? Zero maybe? If so, your search failed to find any matches. You should check to see if j > 0 before the final ReDim & possibly outputing a blank file.
Insomnia is just a byproduct of, "It can't be done"
It's not the file size. Your search routine is not working so you need to work on that. If anything was found, j would not be zero.
The file isn't > 2gb is it?
Insomnia is just a byproduct of, "It can't be done"
Here's a slight twist on westconn's idea. I've commented the code and expect you to examine it to answer your own questions. If you have additional questions, ensure you identify the problem & what line(s) of code that are causing the problems.
Code:
Private Function FindData(FileName As String, FieldName As String, Criteria As String)
Dim file1 As String, outList As String
Dim lHeader As Long
Dim lPtr As Long, lStopPtr As Long
Const sHeader As String = "UMTSGSMPLMNCallDataRecord"
' add error trapping here so if you pass bad filename or error opening file, you'll know
lStopPtr = FreeFile()
Open FileName For Input As #lStopPtr
file1 = Input(LOF(1), #lStopPtr)
Close #lStopPtr
If Len(file1) = 0 Then Exit Function
Do
' find next occurrence of the FieldName
lPtr = InStr(lPtr + 1, file1, FieldName)
If lPtr = 0 Then Exit Do ' if not found, done searching file
' found field name see if record matches criteria
' determine where end of line stops, which should be a carriage return
lStopPtr = InStr(lPtr + 1, file1, vbCrLf)
If lStopPtr = 0 Then lStopPtr = Len(file1) + 1
' now search just that part of the file, extracting a line to test
If InStr(Mid$(file1, lPtr + Len(FieldName), lStopPtr - lPtr), Criteria) Then
'found it
' get the header start
lPtr = InStrRev(file1, sHeader, lPtr)
If lPtr = 0 Then lPtr = 1
' get the next header
lStopPtr = InStr(lStopPtr, file1, sHeader)
If lStopPtr = 0 Then lStopPtr = Len(file1) + 1
' extract stuff between headers
outList = outList & Mid$(file1, lPtr, lStopPtr - lPtr)
Else
' do nothing. didn't find a match in that field
End If
lPtr = lStopPtr ' move the pointer along
Loop
FindData = outList ' return the result
End Function
' example usage of the above function
Private Sub Command1_Click()
Dim sResults As String
' change file name, field name & criteria as needed. Can use variables vs hardcoding the parameters
sResults = FindData("c:\infile[1].txt", "calledSubscriberIMSI", "404281130154600")
If Len(sResults) Then
MsgBox "Found"
' open a file and write sResults to it
Else
MsgBox "Not found"
End If
End Sub
Edited: If you have questions about some of the VB functions (InStr, InStrRev, Mid, etc), open your help files.
Here's an on-line VB help file, but not exactly the easiest to use
Last edited by LaVolpe; Sep 1st, 2010 at 10:31 PM.
Insomnia is just a byproduct of, "It can't be done"
Dilettante has already given you a complete, compact and flexible solution, all you have to do is to change the code from writing the results into a TextBox to writing them to a file - something you should be able to do for yourself.
Looking at Dilettante's code, it's easy to see where the results are added to the textbox and if you look at the code I posted you can see how to open and write data to a file. In addition, there are plenty of examples / tutorials on file input and output here.
So far I've seen no code that you have written or attempted to write in order to help yourself.
BTW I don't respond to PMs asking for help, I only respond to public requests posted to the forum.