Solved

find and extract data from text file

Posted on 2011-03-16
12
304 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now