By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,805 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,805 IT Pros & Developers. It's quick & easy.

Search a text file

P: 5
hi
Am using the following code to search a textfile.The returned value will always be found in this case, is there any problem in the code??
Expand|Select|Wrap|Line Numbers
  1. Dim list As System.Collections.ObjectModel.ReadOnlyCollection _
  2.         (Of String)
  3.         list = My.Computer.FileSystem.FindInFiles("C:\Find", _
  4.         Search.Text, True, FileIO.SearchOption.SearchTopLevelOnly)
  5.  
  6.  
  7.         Dim i As Integer = 0
  8.  
  9.         For Each name As String In list
  10.             RichTextBox1.Text = "(" & StringSearch.Text & ")" & " " & "Found" & "  " & name
  11.             i = 1
  12.             ' StatusText = " Found "
  13.         Next
  14.  
  15.  
  16.         If i = 0 Then
  17.             ' StatusText = " Not Found "
  18.             RichTextBox1.Text = StringSearch.Text & "  " & " Not Found"
  19.         End If
Aug 6 '07 #1
Share this Question
Share on Google+
8 Replies


hariharanmca
100+
P: 1,977
Am using the following code to search a textfile...
you can use FileExist to get it. do not use any for loop.
Aug 6 '07 #2

Expert 5K+
P: 8,434
you can use FileExist to get it. do not use any for loop.
Although I'm unfamiliar with the VB.Net syntax, I think the purpose here is to search a bunch of files to find those which contain a given string. I don't believe FileExist will provide that functionality.

Please correct me if I'm mistaken.
Aug 7 '07 #3

Expert 5K+
P: 8,434
Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer = 0
  2. For Each name As String In list
  3.   RichTextBox1.Text = "(" & StringSearch.Text & ")" & " " & "Found" & "  " & name
  4.   i = 1
  5. Next
  6. If i = 0 Then
  7.   RichTextBox1.Text = StringSearch.Text & "  " & " Not Found"
  8. End If
I assume the purpose of the above code is to determine whether there are any entries in the list collection. Can't you just check list.Count or something? At the very least, you should Exit For immediately rather than continuing to loop through all entries.

If this were a VB6 collection, I would expect to code something like
Expand|Select|Wrap|Line Numbers
  1. If list.Count = 0 Then
  2.   RichTextBox1.Text = StringSearch.Text & "   Not Found"
  3. End If
Oops! I see, you were trying to list the names of the files which were found. Well, your existing code will show the last name, not all of them. What you need to do is, each time around the For loop, add the name onto the end of RichTextBox1.Text, not replace it entirely.

So, perhaps something like this would make more sense...
Expand|Select|Wrap|Line Numbers
  1. If list.Count = 0 Then
  2.   RichTextBox1.Text = StringSearch.Text & "   Not Found"
  3. Else
  4.   For Each name As String In list
  5.     RichTextBox1.Text = RichTextBox1.Text & "(" & StringSearch.Text & ")" & " Found  " & name & vbNewLine
  6. Next
  7. End If
Aug 7 '07 #4

P: 5
hello,

thanks for your reply but, this code is returning for me an always " Found" . What i am trying to do is to search a text file, for example "iman", if he found this string it will return found for me, if not it will return " not found " . Am always getting a found,which is sometimes false
thanks
Aug 8 '07 #5

hariharanmca
100+
P: 1,977
hello,

thanks for your reply but, this code is returning for me an always " Found" . What i am trying to do is to search a text file, for example "iman", if he found this string it will return found for me, if not it will return " not found " . Am always getting a found,which is sometimes false
thanks
anyhow, you are using file system object. just run a 'for loop' to search bunch of files with sysObjName.FileExist(<strFilePath>).
Aug 8 '07 #6

Expert 5K+
P: 8,434
anyhow, you are using file system object. just run a 'for loop' to search bunch of files with sysObjName.FileExist(<strFilePath>).
I assumed that the My.Computer.FileSystem.FindInFiles was achieving that.
Aug 8 '07 #7

hariharanmca
100+
P: 1,977
I assumed that the My.Computer.FileSystem.FindInFiles was achieving that.
Okay, but i never tryed that. And I don't have VB to try new.
Aug 8 '07 #8

Expert 5K+
P: 8,434
Okay, but i never tryed that. And I don't have VB to try new.
I've never seen or heard of it, either. I was just guessing, based on the name and context.
Aug 8 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.