Solved

find and extract data from text file

Posted on 2011-03-16
12
307 Views
Last Modified: 2012-05-11
Hi all,

I have a text file named test.txt and it contains many lines (can be from 10 to 10,000 lines). I would like for it to search for the phrase "You finished in  position :" and return in label1 the data that is after this text. There is always only 1 occurence of this sentence in teh file.

I am using VB 2005. I have searched the databse and its eems i coudln't find what i was looking for.

Can somebody help?

thx,

Si
0
Comment
Question by:touyets17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35146811
1- get the contents of text file in a string:
Dim TempString  as String
TempString =  File.ReadAllText( "test.txt")
2- lfind the keyword:
dim Keyword as string:
keyword = Mid(str, InStr(TempString,  "You finished in  position :" ), 28 + x)
'where  x is length of string you want to find.
3-
label1..text  = microsoft.visualbasic.right(keyword ,x)
0
 

Author Comment

by:touyets17
ID: 35147588
Thanks LeDaouk but your code does not work for me.
0
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35147612
attach the file please
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 17

Expert Comment

by:Zhaolai
ID: 35150309
Try this:
Dim strAllText As String = System.IO.File.ReadAllText("c:\test.txt")
Dim strSearch As String = "You finished in  position :"
Label1.Text = strAllText.Substring(strAllText.LastIndexOf(strSearch) + strSearch.Length)

Open in new window

0
 
LVL 3

Expert Comment

by:1303gun
ID: 35157321
Just a question: when you sayed that you want to read the content after "You finished in position :", did you mean the content until the end of the line or until the end of the file?
0
 

Author Comment

by:touyets17
ID: 35163724
Hi Zhaolai, that's almost perfect.
As 1303gun asked, i might not have made it clear before but i just want up until teh end of that line, not the whole text document. How would I adapt Zhaolai's code?
0
 

Author Comment

by:touyets17
ID: 35164946
i have attached the text file.
test.txt
0
 
LVL 4

Accepted Solution

by:
LeDaouk earned 250 total points
ID: 35164988
0
 
LVL 3

Assisted Solution

by:1303gun
1303gun earned 125 total points
ID: 35165221
You can also try this alternative. It's attached.

The project was made on Visual Studio 2010.
LineReader.zip
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 125 total points
ID: 35165515
Try this:

        Dim strAllText As String = System.IO.File.ReadAllText("c:\test.txt")
        Dim strSearch As String = "You finished in  position :"
        Dim strTemp As String = strAllText.Substring(strAllText.LastIndexOf(strSearch) + strSearch.Length)
        Label1.Text = strTemp.Substring(0, strTemp.IndexOf(vbCrLf))
0
 

Author Comment

by:touyets17
ID: 35165607
Thanks LeDaouk, I adapted your code and it works perfectly!

Thanks to Zhaolai and 1303qun as wella sit works really well as well. I like 1303qun's approach of underloading the memory cache. I will defo use it if in the future I have really long text files but i don't see this happening in this project but you never know!

0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question