Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file as below:

BRANCH :SARDAR BHILADWALA PARDI PEOPLE'S CO-OP. ,Head Office NODE : 9
BRANCH CODE : 1 RUN DATE :11/05/2009 USER : SNA
REPORT ID :R048260 * AREA WISE MEMBER LISTING OF SHARE CUSTOMERS ** PAGE : 1
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Selection :- Product Code : SHM SHARE CAPITAL
All/Selected/Range : A ALL
Specific Area Code :
From Area Code :
To Area Code :
All/Selected/Range : A ALL
Specific Sub Area Code :
From Sub Area Code :
To Sub Area Code : 9999 **********
Skip Closed Accts : Y YES
As On Date : 31/03/2009
------------------------------------------|
| Area Code : 1 PARDI |
------------------------------------------|
| Sub Area Code : |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Sub Sub Area Code: |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Sr.No | Membership No | Name | Addresss | Occupation |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| | 35957 | SHAH HEMANGINI KANAIYALAL NATVARLAL |SHROFF STREET,, KILLA PARDI, DIST.VALSAD, Pardi, Pin Cd : 396125 |
| 1 | 35959 | SARSIWALA SAKINABEN ASGARALI ABDULHUSEIN |OPP.HIGHPOWER SODA FACTORY,, DUNGRI FALIA,DAMNI ZAPA,, KILLA PARDI.DIST.VAL |
| 2 | 36022 | PATEL BHARTIBEN BABUBHAI KHANDUBHAI |B/H BALMANDIR,, KANSARWAD,, KILLA PARDI., Pardi, Pin Cd : 396125 |
| 3 | 36023 | PATEL ASHISHKUMAR BABUBHAI KHANDUBHAI |B/H BALMANDIR,, KANSARWAD,, KILLA PARDI., Pardi, Pin Cd : 396125 |
| 4 | 36025 | SHAIKH HALIMABIBI UMARBHAI MOHMEDBHAI |OPP.JALARAM KHAMAN HOUSE,, CHIVAL ROAD,, KILLA PARDI.DIST.VALSAD, Pardi, Pi |
| 5 | 36072 | LAD PUSHPABEN VINODBHAI BHANABHAI |KUMBHARWAD,, VALSADI ZAPA,, KILLA PARDI,DIST.VALSAD, Pardi, Pin Cd : 396125 |
| 7 | 36076 | KOTHARI PANNABEN JANAKBHAI ISHVARLAL |VANIAWAD,, KILLA PARDI, DIST.VALSAD, Pardi, Pin Cd : 396125 |
| 8 | 36080 | PATEL DHARMESH BHAGUBHAI KANJIBHAI |NEAR RATAN RICE MILL,, DAMNI ZAPA,, KILLA PARDI., Pardi, Pin Cd : 396125 |
| 9 | 36081 | MAPARA BANKIMKUMAR SUNDARLAL HARJIVANDAS |BALAKHADI,, STATION ROAD,, 

From this text file I want to read only "AREA CODE :" word and then the text after "AREA CODE :" needs to be stored in a variable.

I am able to read file also find the word but my problem is I am getting all lines which contain "AREA CODE:".Instead I need only "AREA CODE : 1 PARDI" from text file...
My current code
[Added code from OPs comment] Milind
VB
objStreamReader = New StreamReader("E:\Rachna\ELECTION 2009-SHAREHOLDERS AREAWISE.txt") 
strLine = objStreamReader.ReadLine 
Do While Not strLine Is Nothing 
'MsgBox(strLine) 
If strLine.ToUpper.Contains("Area Code :".ToUpper)  Then 
   strLine.Trim() 
   strAreaCode = Split(strLine, " ") 
End If 
'Dim var As String = strLine.StartsWith("Area Code :") 
strLine = objStreamReader.ReadLine 
Loop 
objStreamReader.Close() 


This is code where File is read and "AREA CODE :" word is found. But it retuns line "From Area Code :" from txtfile which I dont need.
Can any1 please help me?
Posted
Updated 22-Nov-12 19:34pm
v2
Comments
MT_ 23-Nov-12 1:08am    
It will do more good, if you put code snippet what you have done so far to get "AREA CODE: ". That way it will narrow down the problem and people can suggest if anything is wrong in that code.
Rachna0309 23-Nov-12 1:14am    
objStreamReader = New StreamReader("E:\Rachna\ELECTION 2009-SHAREHOLDERS AREAWISE.txt")
strLine = objStreamReader.ReadLine
Do While Not strLine Is Nothing
'MsgBox(strLine)
If strLine.ToUpper.Contains("Area Code :".ToUpper) Then
strLine.Trim()
strAreaCode = Split(strLine, " ")
End If
'Dim var As String = strLine.StartsWith("Area Code :")
strLine = objStreamReader.ReadLine
Loop
objStreamReader.Close()

This is code where File is read and "AREA CODE :" word is found.
But it retuns line "From Area Code :" from txtfile which I dont need.

VB
Dim StrAreaCode As New List(Of String) 'collect area-codes

       Dim objStreamReader As New IO.StreamReader("E:\a.txt")
       Dim strLine = objStreamReader.ReadLine

       'Declare table add columns
       Dim ReadGridFlag = -1

       Do While Not strLine Is Nothing
           If strLine.Trim.ToUpper.StartsWith("Area Code :".ToUpper) Then
               strLine.Trim()
               StrAreaCode.Add(strLine.Split(" :")(1)) 'ADD AreaCode in list
           End If
           strLine = objStreamReader.ReadLine

           If strLine.Trim.ToUpper.StartsWith("| Sr.No".ToUpper) Then
               ReadGridFlag = 0
           ElseIf ReadGridFlag = 1 Then
               'split text with "|" and
               'add row in table
               'now assign splitted values to respective cell in table
           ElseIf strLine.Trim.ToUpper.StartsWith("|--".ToUpper) And ReadGridFlag = 0 Then
               ReadGridFlag = 1
           End If
       Loop
       objStreamReader.Close()

Happy Coding!
:)
 
Share this answer
 
v3
Comments
MT_ 23-Nov-12 1:50am    
Wouldn't that bring line with Sub Area Code as well ? And OP dosn't want that.
Aarti Meswania 23-Nov-12 2:00am    
oh! yes, you are right, he needs only "Area Code :"
please see updated solution
thank you!
:)
Rachna0309 23-Nov-12 2:02am    
Thanx everyone for your help.Solved my problem.
Aarti Meswania 23-Nov-12 2:04am    
welcome! :)
Rachna0309 23-Nov-12 2:05am    
Now for this same text file I want all SRNO,membership no,name and address to be stored in database.how to solve this?
I would simply replace one line of your code and it should work fine.

Write
VB
If strLine.ToUpper.Contains("| Area Code :".ToUpper)  Then

intead of

VB
If strLine.ToUpper.Contains("Area Code :".ToUpper)  Then


And you should be fine.
Milind
 
Share this answer
 
Comments
Rachna0309 23-Nov-12 2:08am    
Milind:Thanx for that but I have already done it.
MT_ 23-Nov-12 2:10am    
Good. Though you may have solve the problem yourself, I would suggest to appreciate CP members who took time to help you. You can atleast upvote them.
Rachna0309 23-Nov-12 2:18am    
yupp sure...done.Can You'll please solve my another problem?
Now for this same text file I want all SRNO,membership no,name and address to be stored in database.how to solve this?
Here is a sample approach.
Read the contents of the file into a string and then use the split [^] method to separate each line of the file.
Then loop through each line of the file and check if that line starts with the word "Area Code" to retrieve the area code. Here is the code sample

Dim AreaCode As String = ""
Using sr As New StreamReader("D:\Test\Test.txt")
	Dim strFileData As String = sr.ReadToEnd()
	Dim strInputLines As String() = strFileData.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
	For Each Data As String In strInputLines
		If Data.Trim().ToUpper().StartsWith("AREA CODE") Then
			Dim AreaCodeLine As String() = Data.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
			If AreaCodeLine.Length > 1 Then
				AreaCode = AreaCodeLine(1)
			End If
		End If
	Next
End Using



PS: I agree with Milind. You should be more specific in your question on what have you tried so far and also post your code when you have tried something.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900