Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with searching a text file and displaying

Status
Not open for further replies.

Buddy008

Programmer
Sep 29, 2003
11
US
My program opens a file as a string, and I need to know how to use a text box to find a word in the string, and then display all the instances it finds to a listbox.
Can anyone help me out here?
 
have you considered regular expressions!

see


for more info

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
come on... get involved!
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
Ok, I'm not searching for files, I'm trying to find text in a file... Now I used to know how to do this, but it's been 3 years since I've messed around with this stuff... Here is what I got for Open:
wrap$ = Chr$(13) + Chr$(10)
CommonDialog1.Filter = "Text files (*.TXT) | *.TXT"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> &quot;&quot; Then
Open CommonDialog1.FileName For Input As #1
Do Until EOF(1)
Line Input #1, alltext$
file$ = file$ + alltext$ + wrap$
Loop
Close #1
MsgBox (&quot;Your File Has Been Loaded!&quot;), vbInformation, &quot;NOTAMS&quot;
Else
Exit Sub
End If
I have a menu item that brings up an inputbox and there the user selects what they want to view(between 3 things POC, compname ,and notam). In my add I concatenate all 3 of these things into one line of text which saves into the file they have opened in the above code.
After the user enters their choice, I want the results to show up in a listbox. I know there is a way to sort through a string, but I can't remember if I have to create some sort of array or not(that's where the 3 years hits me)
If anyone could help me out, it would be very much appreciated!

Josh
 
Why do you use the lines
Do Until EOF(1)
Line Input #1, alltext$
file$ = file$ + alltext$ + wrap$
Loop

You should use the textstring readall command ie text$ = file.readall

This will make one string for the whole file. The user then wants to search for a particular string you can then split the string into however many lines, using the same array each time. When the loop finds the string you are looking for, it will then post the instance into the listview box.

Is this what you are trying to achieve?

BB
 
you can use split to seperate your POC, compname ,and notam if u have a deliminator!

or you could see the below code (a bit overkill given your situation but.. hey if it helps!!)

(needs a ref to microsoft VBscript regular expressions)

Dim RE As RegExp
Dim MC As MatchCollection
Dim M As Match
Dim mystring As String
Dim mysearch As String

Private Sub Command1_Click()
'sample of data entry
mystring = &quot;POC1&quot; & &quot;compname1&quot; & &quot;notam1&quot; & vbCrLf & _
&quot;POC1&quot; & &quot;compname2&quot; & &quot;notam2&quot; & vbCrLf & _
&quot;POC1&quot; & &quot;compname3&quot; & &quot;notam3&quot; & vbCrLf & _
&quot;POC2&quot; & &quot;compname1&quot; & &quot;notam4&quot; & vbCrLf & _
&quot;POC2&quot; & &quot;compname2&quot; & &quot;notam5&quot; & vbCrLf & _
&quot;POC3&quot; & &quot;compname1&quot; & &quot;notam6&quot; & vbCrLf & _
&quot;POC3&quot; & &quot;compname2&quot; & &quot;notam7&quot; & vbCrLf & _
&quot;POC3&quot; & &quot;compname3&quot; & &quot;notam8&quot; & vbCrLf
'get the search criteria
' mysearch = InputBox(&quot;Enter a search criteria.&quot;, &quot;Enter a search criteria.&quot;)
' mysearch = &quot;compname1&quot;
mysearch = Text1.Text 'validate this string in whatever way you need!

FindMe mystring, mysearch

End Sub

Private Sub FindMe(StringToSearch As String, StringToFind As String)

Set RE = New RegExp

StringToFind = &quot;.*&quot; & StringToFind & &quot;.*&quot; & &quot;\n&quot; 'padded because i dont know how your string is deliminated

RE.IgnoreCase = True
RE.Global = True
RE.Pattern = StringToFind

Set MC = RE.Execute(StringToSearch)

For Each M In MC
' Debug.Print &quot;Value Reads:&quot; & M.Value
List1.AddItem M.Value
Next M

End Sub


Private Sub Form_Load()
Text1 = &quot;compname1&quot;
End Sub

like i said.. overkill but hey! it may help!

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
come on... get involved!
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
Hey thanks a lot!!
BiggerBrother the reason I use those lines is because the last time I had to write a progam, I was using visual basic 2.0, and I'm not real familiar with all the new stuff in 6.0 . I have basically been using trial and error to see if the stuff that I used to know works in 6. Most of the code still works, even though it may seem kind of old and ridiculous.
Once again thanks for your help!

Josh
 
BiggerBrother could you give me an example of yours??
 
>How do I reference a VBscript?? (regexp?!?!?)

in the IDE...

project->references then tick the &quot;mivrosoft VBScript Regular expression&quot; (5.5 i think) library box

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
come on... get involved!
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
refernce: microsoft script control 1.0

dim myString as string

Sub form_load()
myString = inputbox(&quot;Please enter a string to search for:&quot;)
if mystring <>&quot;&quot; then
Call FindObject
else
exit sub
end if

end sub

Sub FindObject()
dim newFSO as new filesystemobject
dim ts as textstream
dim entry as string
dim var(3) as string 'variable to assign your lines to.
dim i as integer ' count

set ts = newFSO.OpenTextFile(&quot;myTextFile.txt&quot;, ForReading)
Do
entry = ts.readline
var() = split(entry, &quot;,&quot;) ' split the returned string into 3, separated by in this case a comma.
for n=1 to 3
if var(n)=mystring then
myListBox.additem Var(1) & &quot; &quot; & Var(2) & &quot; &quot; Var(3)
end if
next n

loop until ts.atendofstream
ts.close

if myListBox.ListCount = 0 then
msgBox &quot;Sorry, But no matches for your entry were found&quot;

END IF

end sub


Hope this helps. I hope you have used delimiters, otherwise, you can search the string. Try this:

Sub FindObject()
dim newFSO as new filesystemobject
dim ts as textstream
dim entry as string
dim var(3) as string 'variable to assign your lines to.
dim i as integer ' count

set ts = newFSO.OpenTextFile(&quot;myTextFile.txt&quot;, ForReading)
Do
entry = ts.readline
if instr(1,entry,mystring) = true then
myListBox.additem entry
end if

loop until ts.atendofstream
ts.close

if myListBox.ListCount = 0 then
msgBox &quot;Sorry, But no matches for your entry were found&quot;

END IF

end sub


This should work with or without delimiters!

Good Luck
BB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top