473,569 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check whether a given string exists in a file

kd
Hi All,

Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time and
then check for the given string?

Thanks.
kd
Nov 21 '05 #1
10 2699
"kd" <kd@discussions .microsoft.com> schrieb:
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time
and
then check for the given string?


No. You can use 'ReadToEnd' to read the whole file into a string and use
'InStr' to check for the pattern, but this is only recommended for small
files.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

"kd" <kd@discussions .microsoft.com> schrieb im Newsbeitrag
news:93******** *************** ***********@mic rosoft.com...
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time and then check for the given string?


Well, you could do something like

Private Function FindInFile(ByVa l searchPattern As String, ByVal path As
String) As Boolean
Dim sr As IO.StreamReader
Try
sr = New IO.StreamReader (IO.File.OpenRe ad(path))
Return (sr.ReadToEnd.I ndexOf(searchPa ttern) > -1)
Catch ex As Exception
Return Nothing
Finally
sr.Close()
End Try
End Function

but I doubt that this would be faster then checking line by line and
breaking if a match occurs. That could be easily tested though...
Nov 21 '05 #3
KD,

Do you mean with text "text" or ".txt". Because the answers you have got now
are for ".txt".
For "text" there is not *one* method, because you don't know the format of
the saved file.

I hope this helps?

Cor
Nov 21 '05 #4
kd
Thanks

What would you suggest the recommended size of the .txt file, in order to use
'ReadToEnd'?

kd
"Herfried K. Wagner [MVP]" wrote:
"kd" <kd@discussions .microsoft.com> schrieb:
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time
and
then check for the given string?


No. You can use 'ReadToEnd' to read the whole file into a string and use
'InStr' to check for the pattern, but this is only recommended for small
files.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
kd

Thanks for the code.

kd

"Diana Mueller" wrote:

"kd" <kd@discussions .microsoft.com> schrieb im Newsbeitrag
news:93******** *************** ***********@mic rosoft.com...
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time

and
then check for the given string?


Well, you could do something like

Private Function FindInFile(ByVa l searchPattern As String, ByVal path As
String) As Boolean
Dim sr As IO.StreamReader
Try
sr = New IO.StreamReader (IO.File.OpenRe ad(path))
Return (sr.ReadToEnd.I ndexOf(searchPa ttern) > -1)
Catch ex As Exception
Return Nothing
Finally
sr.Close()
End Try
End Function

but I doubt that this would be faster then checking line by line and
breaking if a match occurs. That could be easily tested though...

Nov 21 '05 #6
kd
It is a .txt file.
kd

"Cor Ligthert" wrote:
KD,

Do you mean with text "text" or ".txt". Because the answers you have got now
are for ".txt".
For "text" there is not *one* method, because you don't know the format of
the saved file.

I hope this helps?

Cor

Nov 21 '05 #7
kd

I mean what would be the maximum size that you suggest for the .txt file in
order to use 'ReadToEnd'?

kd

"kd" wrote:
Thanks

What would you suggest the recommended size of the .txt file, in order to use
'ReadToEnd'?

kd
"Herfried K. Wagner [MVP]" wrote:
"kd" <kd@discussions .microsoft.com> schrieb:
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time
and
then check for the given string?


No. You can use 'ReadToEnd' to read the whole file into a string and use
'InStr' to check for the pattern, but this is only recommended for small
files.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
"kd" <kd@discussions .microsoft.com> schrieb:
I mean what would be the maximum size that you suggest for the .txt file
in
order to use 'ReadToEnd'?


Some KB :-). Note that the file will be loaded into memory and thus the
memory usage will be incremented by at least the size of the file.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
Addendum:

Code for reading a file line-by-line:

Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&la ng=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4331
by: OvErboRed | last post by:
Hi, I'm trying to determine whether a given URL exists. I'm new to Python but I think that urllib is the tool for the job. However, if I give it a non-existent file, it simply returns the 404 page. Aside from grepping this for '404', is there a better way to do this? (Preferrably, there is a solution that can be applied to both HTTP and FTP.)...
10
4499
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is "ENOENT", The file doesn't exist.
7
33458
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
2
1294
by: Ponnurangam | last post by:
Hi, I need to check whether a file named flower exists with different extensions. I know only file name. I don't the the extension(may be .bmp, .ipg, or ..gif). And if there are files with different extensions, I need to get their name with extensions and copy them to a folder.
3
31407
by: laredotornado | last post by:
Hello, Using PHP 4, how do you check if a file exists on an FTP server given its connection information and a directory to check? Thanks, - Dave
5
11085
by: sword | last post by:
How can I check whether a file exists?
3
4161
by: trint | last post by:
How can I do this with my c# code with my website(because the file is there, but the code doesn't return it)?: if(File.Exists(String.Format("~/images/categories/{0}", sFileName)) return String.Format("~/images/categories/{0}", sFileName); else if(File.Exists(String.Format("~/images/products/{0}", sFileName)) return...
3
14541
Maidenz08
by: Maidenz08 | last post by:
How do i check whether an email id exists or not? I am following a three step validation process.. 1) syntax validation- which is pretty straight forward 2) DNS validation - I'm able to do this too. 3) mailbox validation - I have read in many forums saying it is IMPOSSIBLE to check whether a particular email id exists. But take a look...
12
2223
by: foolsmart2005 | last post by:
There are 10 webpages on the host, e.g. 001.html, 002.html, 003,html, 004.html, ......010.html I want to check whether the page is the last page. How can I do. In the index.html -Go to last page,<---- I want to link this to the last page(010.html). Can I do it? How? Thanks!
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.