[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

find and extract data from text file

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
touyets17
Asked:
touyets17
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
LeDaoukCommented:
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
 
touyets17Author Commented:
Thanks LeDaouk but your code does not work for me.
0
 
LeDaoukCommented:
attach the file please
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ZhaolaiCommented:
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
 
1303gunCommented:
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
 
touyets17Author Commented:
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
 
touyets17Author Commented:
i have attached the text file.
test.txt
0
 
LeDaoukCommented:
0
 
1303gunCommented:
You can also try this alternative. It's attached.

The project was made on Visual Studio 2010.
LineReader.zip
0
 
ZhaolaiCommented:
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
 
touyets17Author Commented:
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!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now