Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract text string from text file 1

Status
Not open for further replies.

rockfish12

Technical User
Feb 12, 2002
31
US
What i would like to know is how to read a text file - located at, say, c:\windows\desktop\file.txt - that has a bunch of information in it, but only extract a few particular strings from it.

Though I won't know the exact string values that I want ahead of time, they will always be numbers that will always be set off by a specific phrase/text string, like so:

RANDOM TEXT ABOVE
RANDOM TEXT ABOVE
West_Bounding_Coordinate: -117.125
East_Bounding_Coordinate: -117.000
North_Bounding_Coordinate: 32.625
South_Bounding_Coordinate: 32.500
RANDOM TEXT BELOW
RANDOM TEXT BELOW

Basically, I want to be able to extract the numbers - in this case, -117.125, -117.000, 32.625, and 32.500 - and store them in fields in an Access table.

Any advice on how to do this?
 
Hi,
Dim str As String, str1() As String
Open "C:\windows\desktop\file.txt" For Input As #1
While (Not EOF(1))
Line Input #1, str
str1() = Split(str, ":")
Select Case str1(1)
Case "117.125"
'store in field
Case "-117.000"
' store
Case "32.625"
'store
Case "32.500"
'store
End Select
Wend
Close #1

Jon
 
Tks, Jon4747...that is helpful.

However, I am going to be extracting different numbers from different text files each time. Is there a way to modify that code so that it will be more generic, and extract any number after the desired text string, no matter what that number is?

For instance, let's say I must periodically overwrite C:\windows\desktop\file.txt with a nearly identical file, whose only difference is that the numbers are different, like this:

RANDOM TEXT ABOVE
RANDOM TEXT ABOVE
West_Bounding_Coordinate: -200.00
East_Bounding_Coordinate: -600.000
North_Bounding_Coordinate: 58.00
South_Bounding_Coordinate: 96.00
RANDOM TEXT BELOW
RANDOM TEXT BELOW

Note that the numbers are different from the original example.

How can I modify the code so that it will take any number that follows each of those four strings, "West_Bounding_Coordinate:", "East_Bounding_Coordinate:", "North_Bounding_Coordinate:", and "South_Bounding_Coordinate:"?
 
Hi,
Str1(0) = Trim(Str1(0))'remove any leading/trailing spaces
Select Case str1(0)
Case "West_Bounding_Coordinate"
'code
etc....
Or,
Dim StrW as String
Dim StrE as String
Dim StrN as String
Dim StrS as String
StrW = "West_Bounding_Coordinate"
etc...

Str1(0) = Trim(Str1(0))'remove any leading/trailing spaces
If StrComp(StrW,Str1(0)) = True Then
' code
If StrComp(StrE,Str1(0)) = True Then
' code
etc...

I added the Trim statement because I noticed that Str(1) is
" -200.00" and not "-200.00"

Hope this helps

Jon




 
Thanks for your help. This follow-up question is going to give away my utter ignorance.

I'm a total newbie to programming, so I'm trying to test out the Split function that you used in one of your code snippets to see how it works. However, I keep getting an "Invalid Procedure Call or Argument" when using that function.

Is there a type library that I have to enable or something to be able to use the Split function?

I'm pretty sure that the test code that I'm using is good, because I tested it with a custom function nearly identical to Split, and it works. I've included it below.

Dim str As String, strsplit() As String
Open "C:\windows\desktop\file.txt" For Input As #1
While (Not EOF(1))
Input #1, str
strsplit = Split(str, ":")
Debug.Print strsplit(0)
Debug.Print strsplit(1)
Wend
Close #1





 
Hi,
Change
While (Not EOF(1))
Input #1, str
to
While (Not EOF(1))
Line Input #1, str

to input the whole line in

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top