Solved

find and extract data from text file

Posted on 2011-03-16
12
306 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 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