Solved

find and extract data from text file

Posted on 2011-03-16
12
302 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

22 Experts available now in Live!

Get 1:1 Help Now