Search a text file

Posted on 2004-10-14
Last Modified: 2010-04-23
I am looking for a solution similar to searching ini files for values. I need to translate ID's to a description ithout connecting to a dtabase (that would be too easy). Instead of using a case statement to cover 250 possible combinations, it seems like an approach similar to ini files in VB6 is appropriate. Is there an efficient way to do this in An example of the 2 columns I have to use to search follows. I can export these to a text file with any combination of delimiters, etc that will work.

ID  Description
1   Gas
2   Nitrous Oxide
3   Trinium
4   H2O
5   Silver Nitrate
6   Chromium Sulfate
7   Gold
Question by:JeepGeekin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 28

Expert Comment

ID: 12308654
open the file via a StreamReader, read it all into a string and then search for your phrase by using
the instr-function or the IndexOf-method provided by the string object.

Dim oStream As New System.IO.FileStream("myfile.txt",IO.FileMode.Open)
Dim oReader As New System.IO.StreamReader(oStream)

Dim strText As String= oReader.ReadAll

Dim strPhrase As String="Find this"
If strText.IndexOf(strPhrase,0)=-1 Then
 MsgBox "I found it!"
 MsgBox "Bad luck, not found"
End If

Author Comment

ID: 12308879
Thanks for posting this. It seems like a great way to set a flag, but seems like a work around for returning the following value (the description in my case). I guess try to read from the position returned until the end of the line. Is that how you do this? Do you ever use the API calls to read ini files. Sure there was a sub routine you had to create that was less than easy to follow, but you could feed it the ID & it would return the value each time. You could even group the data & tell it which group to go to. Cool stuff. I wonder why they did away with it in

Also, is it good to read in so much data & loop through it like this? I have to do this on 3 or 4 fields all with about 200 - 300 records each on the load event on the form. Sorry. Have to ask.

Author Comment

ID: 12309077
I'm open to any other suggestions on this one. Anyone have any? Great code, but not sure it's what I'm looking for. Anyone else have a better idea?
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

LVL 13

Expert Comment

ID: 12310210
what about using a Collection
read the file upon initialization

Dim MyList as New Collection

Private Sub InitializeMyList()
    Dim f As Integer
    Dim strLine As String
    Dim ThisText As String
    Dim SplitLine() As String

    f = FreeFile
    Open "C:\Temp.txt" For Input As #f
    'Don't process the first line
    Line Input #f, strLine

    Do Until EOF(f)
        Line Input #f, strLine
        SplitLine = Split(strLine, Space(1), -1, vbTextCompare)

        MyList.AddItem Splitline(Ubound(splitline),Splitline(1)

End Sub

Sub Test ()
'you will have to pass the number you want returned as a string
Msgbox (MyList ("2"))
End Sub


Author Comment

ID: 12310476
Looks interesting. I had some trouble with Opening the text file the way you are doing it. Also with "Line Input" & "MyList.AddItem Splitline(Ubound(splitline),Splitline(1)". Do I need to import anything for these to work? So far I am importing System.IO.

Author Comment

ID: 12310487
Sorry. I have not worked with external files yet. I'm new to
LVL 13

Accepted Solution

Corey2 earned 500 total points
ID: 12311168
Sorry I grabbed an example i had lying around and it was a VB6 example this should get you closer.  It may need a little tweeking.


include system.collections
include system
Include system.IO

Dim MyList as New Collection

Private Sub InitializeMyList()
            Dim SplitLine as arraylist()
            Dim sr As StreamReader = New StreamReader("C:\Temp.txt")
            Dim line As String
            ' Read and display the lines from the file until the end
            ' of the file is reached.
             line = sr.ReadLine()  

                line = sr.ReadLine()
                SplitLine = strline.split(space(1),-1)
                 'the numbers may need adjusting I don't remember if it starts at 1 or 0
                MyList.AddItem Splitline(splitline.count) - 1,Splitline(0)

            Loop Until line Is Nothing
end sub

Author Comment

ID: 12311479
OK. I follow this now. I got most of it working (having trouble with the split routine). Until I get this to work I have one question. How does this know to find the ID I am looking for and return the description? I'l look up collections in a bit, but when you are passing it a "2" in your example - does it know to take the "2", find this value in the 1st column (teh ID) and return the value in the second column (the description)? If so, then looks like a great way to handle this. Hope so.
LVL 13

Expert Comment

ID: 12311586
I think you got it
A collection is an organized set of objects in this case strings there are 3 ways to parse a collection you can do a For...Each...Next loop or you can get them by their itme number (this isn't suggested because this can change when the collection is edited. or you can select them by their Key.  In the code I took the ID from the file and made it the key of the each object so in your example if you call this line

msgbox (mylist("3"))

it would return a message box containing the word "Trinium"


What sort of problems are you having with split
you may have to declare strline as a string I would have tested the code but I don't have a .Net compiler on my CPU here.

Dim Strline as String
LVL 13

Expert Comment

ID: 12311608
(ps if you have a tab delimited file you will have to change the split to use a tab instead of a space)

Expert Comment

ID: 12316135
This is just an idea, why don't you maintain your text file as an ini file data format like -



Nitrous Oxide



Silver Nitrate

Chromium Sulfate


By doing this your can use rich APIs to read, write the data of your text file that has like ini file data format.

Author Comment

ID: 12321233
Thanks for the extra info. It looks like this is what I am looking for.

Author Comment

ID: 12322249
Also, since you asked, I don't follow the parameters you are sending for the split function (ie: "space(1),-1").  I couldn't get my program to take these.

Also, About the API call. I read that these are no longer available in .Net or they are being phased out.
LVL 13

Expert Comment

ID: 12322667
I don't know what you read but I think it is now a function of the string class.

After a bit of reading it looks like the -1 is probably throwing you this error.  Try using 0 or omiting it completely.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

624 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