Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Search a text file

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
0
JeepGeekin
Asked:
JeepGeekin
1 Solution
 
iboutchkineCommented:
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!"
Else
 MsgBox "Bad luck, not found"
End If
0
 
JeepGeekinAuthor Commented:
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.
0
 
JeepGeekinAuthor Commented:
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?
0
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.

 
Corey ScheichDeveloperCommented:
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)
     Loop

End Sub

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

Regards
Corey
0
 
JeepGeekinAuthor Commented:
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.
0
 
JeepGeekinAuthor Commented:
Sorry. I have not worked with external files yet. I'm new to VB.net.
0
 
Corey ScheichDeveloperCommented:
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.

Corey


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()  
            Do

                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
            sr.Close()
end sub
0
 
JeepGeekinAuthor Commented:
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.
0
 
Corey ScheichDeveloperCommented:
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"

Corey

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
0
 
Corey ScheichDeveloperCommented:
(ps if you have a tab delimited file you will have to change the split to use a tab instead of a space)
0
 
KarcOriginCommented:
This is just an idea, why don't you maintain your text file as an ini file data format like -

[ID]
Data

[1]
Gas

[2]
Nitrous Oxide

[3]
Trinium

[4]
H2O

[5]
Silver Nitrate

[6]
Chromium Sulfate

[7]
Gold

?
By doing this your can use rich APIs to read, write the data of your text file that has like ini file data format.
Cheers!
0
 
JeepGeekinAuthor Commented:
Thanks for the extra info. It looks like this is what I am looking for.
0
 
JeepGeekinAuthor Commented:
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.
0
 
Corey ScheichDeveloperCommented:
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.

Corey
0

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now