By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 2,933 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

Searching a txt file for instances of a string

P: n/a
I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Paul H wrote:
I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul


I suppose you could do an Instr() on the word Address and an Instr() on
PostCode. then parse out the lines searching for NewLines.

If "Address: " was 20, then you'd want to add 8 to thatto get the start
of the address.

If "PostCode:" was 50, then you'd want to subract 49 from 28 to get the
length of the address.

Then you'd use Mid() to get the address string. Then you'd need to get
the strings delimited by newline characters.

But I don't understand why you don't use Open/Close,Input to get this
informattion. Read it line by line. Name: starts a new record,
PostCode ends a record. When you hit Address:, read each line till you
hit PostCode to get your address info,

Reading and parsing a text file like yours and doing it with Instr()
seems to be a PITA.
Nov 13 '05 #2

P: n/a
On Fri, 27 May 2005 18:41:48 GMT, Salad <oi*@vinegar.com> wrote:
Paul H wrote:
I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul


I suppose you could do an Instr() on the word Address and an Instr() on
PostCode. then parse out the lines searching for NewLines.

If "Address: " was 20, then you'd want to add 8 to thatto get the start
of the address.

If "PostCode:" was 50, then you'd want to subract 49 from 28 to get the
length of the address.

Then you'd use Mid() to get the address string. Then you'd need to get
the strings delimited by newline characters.

But I don't understand why you don't use Open/Close,Input to get this
informattion. Read it line by line. Name: starts a new record,
PostCode ends a record. When you hit Address:, read each line till you
hit PostCode to get your address info,

Reading and parsing a text file like yours and doing it with Instr()
seems to be a PITA.


I think the OP already has a loop set up to read a new record. The
variable "strCurrentLineOfTextFile" strongly implies that this is so.

Hence, what he needs to do is to implement exactly what you have
described. That would typically be done by a secondary test/loop.
The exact language depends on the "blah blah".

It appears that the current code is intended to allow the blank lines
to be ignored. If that is so, the secondary loop would ensure that
the lines after the Address: line and before the Postcode: line are
used, rather than ignored.

Let's assume that the OP is using a Select statement as the basis of
the blah blah (what follows is aircode ... I am horrible at
remembering off the top of my head what the ending instructions are
for loops and such, so confirm that what I have used is correct before
blaming me)

The result he is after might look something like this:
'set up a boolean variable named boolProcessingAddress
' and set it to false
'set up a boolean variable named boolNOT_AT_EOF
' and set it to true
....
do while boolNOT_AT_EOF
Select Case InStr(1, strCurrentLineOfTextFile, ":")
Case 0
' This line does not have a colon in it (anywhere)
' See whether you are currently processing the group between
the address and the postcode
if boolProcessingAddress then
' this is a new line for the address, go ahead with it
endif
Case else
' Do whatever you currently do with your input lines
' For example, you might test to see whether the line starts
' with PostCode:
' If you find a line that starts with Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = True
' Otherwise, if you find a line that doesn't start with
Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = False
End Select
' do whatever you do to get a new strCurrentLineOfTextFile
' set boolNOT_AT_EOF to false if at end of file
...
loop

Testing for a colon ANYWHERE in the line is a recipe for disaster. If
your other lines have an address with a : buried in there, your entire
routine will crash.

mike
Nov 13 '05 #3

P: n/a

"Mike Preston" <mb******@pacbell.invalid.net> wrote in message
news:42****************@news.INDIVIDUAL.NET...
On Fri, 27 May 2005 18:41:48 GMT, Salad <oi*@vinegar.com> wrote:
Paul H wrote:
I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing
the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul


I suppose you could do an Instr() on the word Address and an Instr() on
PostCode. then parse out the lines searching for NewLines.

If "Address: " was 20, then you'd want to add 8 to thatto get the start
of the address.

If "PostCode:" was 50, then you'd want to subract 49 from 28 to get the
length of the address.

Then you'd use Mid() to get the address string. Then you'd need to get
the strings delimited by newline characters.

But I don't understand why you don't use Open/Close,Input to get this
informattion. Read it line by line. Name: starts a new record,
PostCode ends a record. When you hit Address:, read each line till you
hit PostCode to get your address info,

Reading and parsing a text file like yours and doing it with Instr()
seems to be a PITA.


I think the OP already has a loop set up to read a new record. The
variable "strCurrentLineOfTextFile" strongly implies that this is so.

Hence, what he needs to do is to implement exactly what you have
described. That would typically be done by a secondary test/loop.
The exact language depends on the "blah blah".

It appears that the current code is intended to allow the blank lines
to be ignored. If that is so, the secondary loop would ensure that
the lines after the Address: line and before the Postcode: line are
used, rather than ignored.

Let's assume that the OP is using a Select statement as the basis of
the blah blah (what follows is aircode ... I am horrible at
remembering off the top of my head what the ending instructions are
for loops and such, so confirm that what I have used is correct before
blaming me)

The result he is after might look something like this:
'set up a boolean variable named boolProcessingAddress
' and set it to false
'set up a boolean variable named boolNOT_AT_EOF
' and set it to true
...
do while boolNOT_AT_EOF
Select Case InStr(1, strCurrentLineOfTextFile, ":")
Case 0
' This line does not have a colon in it (anywhere)
' See whether you are currently processing the group between
the address and the postcode
if boolProcessingAddress then
' this is a new line for the address, go ahead with it
endif
Case else
' Do whatever you currently do with your input lines
' For example, you might test to see whether the line starts
' with PostCode:
' If you find a line that starts with Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = True
' Otherwise, if you find a line that doesn't start with
Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = False
End Select
' do whatever you do to get a new strCurrentLineOfTextFile
' set boolNOT_AT_EOF to false if at end of file
...
loop

Testing for a colon ANYWHERE in the line is a recipe for disaster. If
your other lines have an address with a : buried in there, your entire
routine will crash.

mike


I knew I needed a second loop, what I hadn't thought of was the Boolean
variable.I just had a complete mental block. Also, thanks for the tip on NOT
searching for *just* a colon, I have corrected that too.

Many thanks Mike you've been a great help

Paul
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.