Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > Find line in text file, put it into a menu item


Reply
 
Thread Tools Display Modes
  #1  
Old 05-27-2003, 05:36 PM
Phantasmagoria Phantasmagoria is offline
Centurion
 
Join Date: May 2003
Posts: 131
Default Find line in text file, put it into a menu item


Hello,

What I would like to do is have a text file be searched for a string ("Name=" in this case) and read what's to the right of it. The text file would be set up like this:

Name="blah"
Message="I am blah blah blah"

I would like to find each "Name=" string in the file and put whatever is to the right of it into a menu array. How would I go about doing this?
Reply With Quote
  #2  
Old 05-29-2003, 11:26 AM
PWNettle PWNettle is offline
Verbose Coder

Retired Moderator
* Guru *
 
Join Date: Dec 1999
Location: Phoenix, Arizona
Posts: 3,011
Default

One way to approach this is to read the file line by line, parse out the data you need, and dynamically add items to an existing menu control array.

In this example, it is assumed that mnuNames exists (with only one element) with an index of zero, which makes it a control array:

Code:
Private Sub FillMenu() Dim sFileName As String Dim lFile As Long Dim sLine As String Dim sMenuText As String Dim lCount As Long ' Change the filename below to your filename: sFileName = "c:\pathto\yourfile.txt" ' Get a safe file handle. lFile = FreeFile ' Initialize the menu index counter. lCount = -1 ' Open the input file. Open sFileName For Input As #lFile ' Read every line in the file. Do While Not EOF(lFile) Line Input #lFile, sLine If Not Len(sLine) = 0 Then ' Process lines that aren't blank and contain the ' target text (Name=). If InStr(sLine, "Name=") Then ' Parse out the menu text. sMenuText = Mid$(sLine, InStr(sLine, "=") + 1) ' Remove beginning and ending double quotes if present. If Left$(sMenuText, 1) = """" Then sMenuText = Mid$(sMenuText, 2) End If If Right$(sMenuText, 1) = """" Then sMenuText = Mid$(sMenuText, 1, Len(sMenuText) - 1) End If ' Increment the menu index counter. lCount = lCount + 1 ' Create a new menu item (only create if this ' isn't element zero, which already exists). If Not lCount = 0 Then Load mnuNames(lCount) End If ' Populate and make the new element visible. mnuNames(lCount).Caption = sMenuText mnuNames(lCount).Visible = True End If End If Loop ' Close the input file. Close #lFile End Sub

Paul
Reply With Quote
  #3  
Old 05-29-2003, 07:24 PM
Phantasmagoria Phantasmagoria is offline
Centurion
 
Join Date: May 2003
Posts: 131
Default

Thanks!!!
Reply With Quote
  #4  
Old 05-29-2003, 07:50 PM
Phantasmagoria Phantasmagoria is offline
Centurion
 
Join Date: May 2003
Posts: 131
Default

Now, using what I learned from that, I have another question.

Code:
Private Sub mnuNames_Click(Index As Integer) Dim caption As String Dim sFile As String Dim lFile As String Dim line As String caption = mnuNames(Index).caption sFile = "C:\Documents and Settings\mike\Desktop\messages.txt" lFile = FreeFile Open sFile For Input As #lFile Do While Not EOF(lFile) Line Input #lFile, line If Not Len(line) = 0 Then ' Process lines that aren't blank and contain the ' target text. If InStr(line, caption) Then IMClient.LocalState = MSTATE_AWAY End If End If Loop ' Close the input file. Close #lFile End Sub

I used a bit of your code in that :-P

Now, that's about as far as I've gotten. What I want to know now is how to search for the "Message=" string (next line) and pull out what's in the ""s there. I'm a little..stuck on how I would increment the line so that I could pull out the away message. Thanks!
Reply With Quote
  #5  
Old 05-29-2003, 09:10 PM
PWNettle PWNettle is offline
Verbose Coder

Retired Moderator
* Guru *
 
Join Date: Dec 1999
Location: Phoenix, Arizona
Posts: 3,011
Default

It'd kind of depend on how many lines (broke up by carriage returns) the message is.

If it's just one line, then all the processing is already there...just do another

Line Input, sLine

to get the message (since it always appears below the name).

<shrugs>

Paul
Reply With Quote
  #6  
Old 05-30-2003, 10:03 AM
Phantasmagoria Phantasmagoria is offline
Centurion
 
Join Date: May 2003
Posts: 131
Default

Okay, thanks, that worked! =)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation Problem - PLs help urgenlty dpdsouza Installation / Documentation 4 12-02-2004 07:09 PM
Map Editor: need to output more than one item per line to text file Kamochan Game Programming 2 02-24-2003 11:14 PM
VB Menus John General 8 02-05-2002 09:31 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->