1 Attachment(s)
File read & write problem..pls help me..
Hello,
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.
Sample values are given below...
UMTSGSMPLMNCallDataRecord
mSTerminatingSMSinMSC
dateForStartOfCharge 07020D'H
exchangeIdentity "BBSR_MSC_R11"'S
recordSequenceNumber 25510'D
timeForStartOfCharge 17 2 44'BCD
outputType 04'H
switchIdentity 0001'H
callIdentificationNumber 54292'D
chargedParty 00'H
outgoingRoute "OBBKJRO"'S
calledSubscriberIMEI 358364004355120F'TBCD
calledSubscriberIMSI 404281130154600F'TBCD
firstCalledLocationInformation 04F48239DB7157'H
->2|0|63 09'H
calledPartyNumber 11919853313214'TBCD
frequencyBandSupported 00'H
teleServiceCode 21'H
mSCIdentification 11919853099002'TBCD
lastCalledLocationInformation 04F48239DB7157'H
serviceCentreAddress 11919854099060'TBCD
tAC 04020B'H
originForCharging 00'H
messageTypeIndicator 00'H
originatingAddress 0E1C4A47854620'TBCD
UMTSGSMPLMNCallDataRecord
mSTerminatingSMSinMSC
dateForStartOfCharge 07020D'H
exchangeIdentity "BBSR_MSC_R11"'S
recordSequenceNumber 25511'D
timeForStartOfCharge 17 2 44'BCD
outputType 04'H
switchIdentity 0001'H
callIdentificationNumber 54293'D
chargedParty 00'H
outgoingRoute "OBBCUTO"'S
calledSubscriberIMEI 357088005862520F'TBCD
calledSubscriberIMSI 404281130154601F'TBCD
firstCalledLocationInformation 04F48237847937'H
->2|0|63 09'H
calledPartyNumber 11919853313210'TBCD
frequencyBandSupported 00'H
teleServiceCode 21'H
mSCIdentification 11919853099002'TBCD
lastCalledLocationInformation 04F48237847937'H
serviceCentreAddress 11919854099060'TBCD
tAC 04020B'H
originForCharging 00'H
messageTypeIndicator 00'H
originatingAddress 0E1C4A47854620'TBCD
UMTSGSMPLMNCallDataRecord
mSTerminatingSMSinMSC
dateForStartOfCharge 07020D'H
exchangeIdentity "BBSR_MSC_R11"'S
recordSequenceNumber 25512'D
timeForStartOfCharge 17 2 44'BCD
outputType 04'H
switchIdentity 0001'H
callIdentificationNumber 54308'D
chargedParty 00'H
outgoingRoute "OBBBHUO"'S
calledSubscriberIMEI 351119608143270F'TBCD
calledSubscriberIMSI 404281130154600F'TBCD
firstCalledLocationInformation 04F482371F9DDD'H
->2|0|63 09'H
calledPartyNumber 11919853313209'TBCD
frequencyBandSupported 00'H
teleServiceCode 21'H
mSCIdentification 11919853099002'TBCD
lastCalledLocationInformation 04F482371F9DDD'H
serviceCentreAddress 11919854099060'TBCD
tAC 04020B'H
originForCharging 00'H
messageTypeIndicator 00'H
originatingAddress 0E1C4A47854620'TBCD
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
Thnkx & Regards
Sajib
Re: File read & write problem..pls help me..
you can try like
vb Code:
Dim file2() As String
d = vbNewLine & vbNewLine & vbNewLine
f = "calledSubscriberIMSI"
v = "404281130154605F"
Open "c:\temp\infile.txt" For Input As 1
file1 = Split(Input(LOF(1), #1), d)
Close 1
ReDim file2(UBound(file1))
j = 0
For i = 0 To UBound(file1)
pos = 0
pos = InStr(pos + 1, file1(i), f) + Len(f) + 40
If Mid(file1(i), pos, Len(v)) = v Then
file2(j) = file1(i)
j = j + 1
End If
Next
ReDim Preserve file2(j - 1)
Open "c:\temp\outfile.txt" For Output As 1
Print #1, Join(file2, d)
Close 1
change paths and v to suit
Re: File read & write problem..pls help me..
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)..
Is there any way to cope up with this?
Thnkx
Re: File read & write problem..pls help me..
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.
Re: File read & write problem..pls help me..
ya it's giving a blank file..
can u pls modify the code for a big input file?
Re: File read & write problem..pls help me..
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?
Re: File read & write problem..pls help me..
ya in this case j is becoming zero after search..pls tell me how to modify the above code?
Re: File read & write problem..pls help me..
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
Re: File read & write problem..pls help me..
Isn't this a duplicate of this http://www.vbforums.com/showthread.php?t=626102 albeit with significant scope creep from your initial request.
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.
Re: File read & write problem..pls help me..
Quote:
Originally Posted by
Doogle
Isn't this a duplicate of this
http://www.vbforums.com/showthread.php?t=626102 albeit with significant scope creep from your initial request.
...
So far I've seen no code that you have written or attempted to write in order to help yourself.
Really? Wish I would've seen/noticed that post beforehand!