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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am attempting to search a text fi

Status
Not open for further replies.

dlingo

Programmer
Nov 19, 2001
60
US
I am attempting to search a text file for a certain string and execute certain functions if the string occurs. If the string doesn't occur I want to execute something else. I'm using the following code.

Do while txtfile.AtEndOfStream <> True
line = txtfile.Readline
If line <> &quot;&quot; Then
mystr = Split(line, &quot;:&quot;, -1, 1)
trimmed = Trim(mystr(0))

Select Case trimmed
Case &quot;string&quot;
--do something here
Case Else
--do something here
End Select

When I leave out the Case Else statement the functions are executed properly when the string occurs. However, when the Case Else statement is inserted the program is executing the second set of funtions even when the string occurs. What am I doing wrong? Please help. Thanks in advance.
 
I don't think you are doing anything wrong. I used your code and it worked perfectly for me. My copy of your code is listed out below. If it continues to not work, maybe the problem lies in your text file and its format.

######################################################

Sub read_text_file()
'routine to find a string within a text file
Dim fs, txtfile, line, mystr, trimmed
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set txtfile = fs.OpenTextFile(&quot;c:\myfile.txt&quot;)

Do While txtfile.AtEndOfStream <> True
line = txtfile.Readline
If line <> &quot;&quot; Then
mystr = Split(line, &quot;:&quot;, -1, 1)
trimmed = Trim(mystr(0))

Select Case trimmed
Case &quot;string&quot;
MsgBox (&quot;Found&quot;)
Case Else
MsgBox (&quot;Not Found&quot;)
End Select
End If
Loop

End Sub

######################################################

Good Luck,
TopJack.
 
I figured out what I was doing wrong. The code was correct for one run through the text file, but I was searching for multiple strings and when I relooped through the text file I was overwriting the appropiate value with the inappropriate value. I fixed it though. Thanks for you help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top