Go Premium for a chance to win a PS4. Enter to Win


Search a text file

Posted on 2004-10-14
Medium Priority
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 VB.net? 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
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 VB.net?

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?
Technology Partners: 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!

LVL 13

Expert Comment

by:Corey Scheich
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 VB.net.
LVL 13

Accepted Solution

Corey Scheich earned 2000 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

by:Corey Scheich
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

by:Corey Scheich
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

by:Corey Scheich
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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