VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
Go Back  Xtreme Visual Basic Talk > > > VB6 Search a text file for a string of text then read the rest of the file


Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2014, 05:46 AM
gibbsuk gibbsuk is offline
Newcomer
 
Join Date: Jan 2014
Posts: 5
Unhappy VB6 Search a text file for a string of text then read the rest of the file


Hi all,

I came across this website via Google trying to find a solution to a problem I have. I dabble with VB, tweaking existing snippets of code to 'make it fit' but I find myself this time with a blank canvass and no examples to tweak from. That's about the extent of my knowledge. Would anyone be so kind as to help me please?

The problem I have is this:

I have a file called LOG.txt and an example of this file would be:
Code:
useless information
useless information
useless information
useless information
useless information

**************
RESULT SUMMARY
**************
useful information
useful information
useful information
useful information
useful information
useful information
useful information

*******************************************
EOF
I want to read this file and I want to create a string called 'Text1' that contains all the information from the RESULT SUMMARY section of the file, right down to the end of the file.

so effectively, my Text1 string would result in containing this (dropping all of the usless info above it):

Code:
**************
RESULT SUMMARY
**************
useful information
useful information
useful information
useful information
useful information
useful information
useful information

*******************************************
I know it must be simple enough, I just can't find the right combination of code to do my usual 'make it fit' for my purpose.

The theory of what I think I may need to do is this:
  • Do a search for the string within the file
  • Find out the line number
  • Jump up two lines (to go above the asterisks)
  • Then read the rest, line by line

Would this be an effective way of doing this?
EDIT: please note that the information may move up and down the text file and is not consistently on line # x

Thanks in advance for any help given.


EDIT
Following the FAQ, I thought about trying an Array but had no luck:

Code:
Dim MyArray() As String
Dim MyString As String
Dim FF As Integer

FF = FreeFile()
Open ("c:\Reports\log.log") For Binary As FF

ReDim MyArray(LOF(FF))
Get FF, , MyArray

'Break the string by commas
MyArray = Split(MyString, "RESULT SUMMARY")
Text1 = MyArray(1)
Close FF
I thought I'd try and be cheeky and set my delimiter to the text I am looking to split from, but I seem to get a blank result in my string, Text1

Last edited by gibbsuk; 01-30-2014 at 07:13 AM.
Reply With Quote
  #2  
Old 01-30-2014, 07:10 AM
gibbsuk gibbsuk is offline
Newcomer
 
Join Date: Jan 2014
Posts: 5
Default

Following the FAQ, I thought about trying an Array but had no luck:

Code:
Dim MyArray() As String
Dim MyString As String
Dim FF As Integer

FF = FreeFile()
Open ("c:\Reports\log.log") For Binary As FF

ReDim MyArray(LOF(FF))
Get FF, , MyArray

'Break the string by commas
MyArray = Split(MyString, "RESULT SUMMARY")
Text1 = MyArray(1)
Close FF
I thought I'd try and be cheeky and set my delimiter to the text I am looking to split from, but I seem to get a blank result in my string, Text1
Reply With Quote
  #3  
Old 01-31-2014, 03:39 AM
gibbsuk gibbsuk is offline
Newcomer
 
Join Date: Jan 2014
Posts: 5
Default

Sorry for the double posts
Reply With Quote
  #4  
Old 01-31-2014, 12:26 PM
passel's Avatar
passelVB6 Search a text file for a string of text then read the rest of the file passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Assuming that is a true copy of your code..
Without testing I'm not sure what you're getting, but I wouldn't expect the read to have read much of anything from the file.

It looks like you declared MyString to hold the contents of the file, but never read anything into it.
You read into MyArray, which is the part where I don't know what that would do since it doesn't make much sense to my understanding of how VB6 I/O works. You then split MyString, which should be empty, so MyArray would be an array on one item of nothing (I would guess, didn't actually test any of this).

Assuming the code you posted was what you tried, the following is what you meant to try.
Code:
Dim MyArray() As String
Dim MyString As String
Dim FF As Integer

FF = FreeFile()
Open ("c:\Reports\log.log") For Binary As FF

ReDim MyString(LOF(FF))
Get FF, , MyString

'Break the string by "RESULT SUMMARY"
MyArray = Split(MyString, "RESULT SUMMARY")
Text1.Text = MyArray(1)
Close FF
That might get you most of the information you want, although not what you posted.
The first "*********" would be part of the previous string, and the "RESULT SUMMARY" would be removed, as the separators are not part of the strings output by the split.
So, I think Text1.Text would end up starting with a blank line (because of the cr/lf after "RESULT SUMMARY" would still be there), then your second "********", and the rest of the text file
Note: I did change "Text1 =" to "Text1.Text =", because using the default property of a control can cause a problem in some cases (not this particular one), and if you move to VB.Net someday, would not work as they don't have "default properties", so best to not get into the habit of not specifying the property you are setting.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 01-31-2014 at 12:33 PM.
Reply With Quote
  #5  
Old 02-03-2014, 02:42 AM
gibbsuk gibbsuk is offline
Newcomer
 
Join Date: Jan 2014
Posts: 5
Default

That code does make more sense. I think I was just losing myself in what I was doing there.

Thanks for the reply passel, that's really helped a lot. Very much appreciated - all the best.
Reply With Quote
Reply

Tags
reading from .txt files, string manipulation


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
 
VB6 Search a text file for a string of text then read the rest of the file
VB6 Search a text file for a string of text then read the rest of the file
 
-->