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

Check whether a given string exists in a file

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
"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

P: n/a

"kd" <kd@discussions.microsoft.com> schrieb im Newsbeitrag
news:93**********************************@microsof t.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(ByVal searchPattern As String, ByVal path As
String) As Boolean
Dim sr As IO.StreamReader
Try
sr = New IO.StreamReader(IO.File.OpenRead(path))
Return (sr.ReadToEnd.IndexOf(searchPattern) > -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

P: n/a
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

P: n/a
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

P: n/a
kd

Thanks for the code.

kd

"Diana Mueller" wrote:

"kd" <kd@discussions.microsoft.com> schrieb im Newsbeitrag
news:93**********************************@microsof t.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(ByVal searchPattern As String, ByVal path As
String) As Boolean
Dim sr As IO.StreamReader
Try
sr = New IO.StreamReader(IO.File.OpenRead(path))
Return (sr.ReadToEnd.IndexOf(searchPattern) > -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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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&lang=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

P: n/a
Herfried,

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.

Amazing that you know that Hefried, however that is normal with fysical
memory.

With human memory it is something else especially when you have HKW memory.

:-)

Cor
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.