|
-
May 31st, 2001, 04:41 AM
#1
Findfile containing text ?
Hello everybody,
I was wondering if there's an API like window do the find function, with files containing certain text.
I've been using the FindFile and FindNextFile API but how could I search those files containing certain text in it ?
Hope someone could point or help me !
Thanks,
Bjorn
-
May 31st, 2001, 05:06 AM
#2
Registered User
You will have to open the file in binary, read in all the values with the get function.
Convert each byte into a character using chr$ and add all the characters together.
Then just use the like operator to check if the text appears in the file
You may wonder will VB be able to store an entire program in a single variable length string?
Now each byte holds a character, and a variable-length string can hold up to approximately 2 billion (2^31) characters, which means as long as the files are smaller than 2GB it should work.
Well in theory anyway. This is an idea, tell me how it goes.
Perhaps you could post the function if you get it working?
-
May 31st, 2001, 05:33 AM
#3
Registered User
Ok here is the function for you to test... I got impatient waiting.
I have only tested it on one file, so would appreciate it if you could send me feedback on its success/failure across all files.
Code:
Option Explicit
Option Compare Text
Function ContainsText(Path As String, Match As String) As Boolean
'Nucleus
If Len(Dir$(Path)) Then
Dim ba() As Byte, n&, ff&, i&, s$
n = FileLen(Path)
ReDim ba(n - 1) ' Resize Byte arrays To hold all bytes In file
ff = FreeFile
Open Path For Binary Access Read As #ff
Get #ff, , ba() ' read file into Byte array
Close #ff
For i = 0 To UBound(ba)
s = s & Chr$(ba(i)) ' read Each Byte into String
Next
If Left$(Match, 1) <> "*" Then Match = "*" & Match
If Right$(Match, 1) <> "*" Then Match = Match & "*"
ContainsText = s Like Match
End If
End Function
Example of Usage:
Private Sub Command1_Click()
MsgBox ContainsText("d:\tmp\readme.txt", "Animated cursors")
End Sub
Last edited by Nucleus; May 31st, 2001 at 07:16 AM.
-
May 31st, 2001, 07:15 AM
#4
Hi Nucleus,
glad you're so impatient :-)
I've been testing the code, but you need to put option compare text in the beginning of your procedure, cause else it's case sensitive 
The function is very nice, but I got the feeling that it's very slow. Wouldn't there be a faster way ?
Thanks a lot,
Bjorn
-
May 31st, 2001, 07:26 AM
#5
Registered User
I realised I didn't post the option compare text, so I updated the function at exactly the same time you posted .
I cannot think of a faster way to do it than the approach I have given. I think with VB that is just about the limit, perhaps C++ or ASM will do it faster.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|