PC Review


Reply
Thread Tools Rate Thread

Check whether a given string exists in a file

 
 
=?Utf-8?B?a2Q=?=
Guest
Posts: n/a
 
      9th Mar 2005
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
 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      9th Mar 2005
"kd" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
Diana Mueller
Guest
Posts: n/a
 
      9th Mar 2005

"kd" <(E-Mail Removed)> schrieb im Newsbeitrag
news:9370BAF3-8703-48A6-B1A6-(E-Mail Removed)...
> 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...


 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      9th Mar 2005
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


 
Reply With Quote
 
=?Utf-8?B?a2Q=?=
Guest
Posts: n/a
 
      9th Mar 2005

Thanks for the code.

kd

"Diana Mueller" wrote:

>
> "kd" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:9370BAF3-8703-48A6-B1A6-(E-Mail Removed)...
> > 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...
>
>
>

 
Reply With Quote
 
=?Utf-8?B?a2Q=?=
Guest
Posts: n/a
 
      9th Mar 2005
Thanks

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

kd


"Herfried K. Wagner [MVP]" wrote:

> "kd" <(E-Mail Removed)> 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/>
>
>

 
Reply With Quote
 
=?Utf-8?B?a2Q=?=
Guest
Posts: n/a
 
      9th Mar 2005
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
>
>
>

 
Reply With Quote
 
=?Utf-8?B?a2Q=?=
Guest
Posts: n/a
 
      9th Mar 2005

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" <(E-Mail Removed)> 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/>
> >
> >

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      9th Mar 2005
"kd" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      9th Mar 2005
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/>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if character exists in string Lars Brownies Microsoft Access 2 17th Jan 2010 10:16 PM
Check if text string exists Keypad Microsoft Access Form Coding 5 25th May 2009 06:28 PM
Check if file exists... =?Utf-8?B?YWJkcnVtcw==?= Microsoft Excel Programming 3 13th Jul 2007 05:30 AM
Check if query string parameter exists shapper Microsoft ASP .NET 2 7th Oct 2006 02:39 AM
Check if file exists Newbie Microsoft Access Form Coding 3 4th Nov 2003 05:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:47 AM.