473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Search a text file

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
8 2314
hariharanmca
1,977 1GB
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
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
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
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
1,977 1GB
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
Killer42
8,435 Expert 8TB
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
1,977 1GB
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
Killer42
8,435 Expert 8TB
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

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

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
3
by: jdworley | last post by:
Howdy! The search is a 1 GB+ file from a crash to be exact - had an e-mail correspondence (window) hopefully in it so I saved the file on reboot before windoze kicked in and want to open and...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
2
by: richardkreidl | last post by:
I want to be able to delete and search for elements in a XML file, I'm using the code below for adding elements which works great: Public Sub cmdAddElement_Click(ByVal sender As System.Object,...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
5
by: Mark | last post by:
Hi I have an application (in vb.NET 2005) which holds data in SQL Server and some of the SQL records are simply paths to related files. I would like to be able to do a text search on both the...
1
by: nganglove | last post by:
C++ string search -------------------------------------------------------------------------------- Hello, please can any one help me? I am given an assigment in C++ to read a text file and...
3
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.