Find line in text file, put it into a menu item

Phantasmagoria
05-27-2003, 05:36 PM
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?

PWNettle
05-29-2003, 11:26 AM
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:

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

Phantasmagoria
05-29-2003, 07:24 PM
Thanks!!!

Phantasmagoria
05-29-2003, 07:50 PM
Now, using what I learned from that, I have another question.

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!

PWNettle
05-29-2003, 09:10 PM
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

Phantasmagoria
05-30-2003, 10:03 AM
Okay, thanks, that worked! =)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum