|
-
Feb 4th, 2013, 05:42 AM
#1
Thread Starter
Lively Member
File Content Search
Hi all,
expecting some ideas to create an application which search for the contents in a file.
Microsoft Windows Search 4.0 or the Search included in Windows 7 does the same.
I have already implemented the Windows Search so I am not looking for the Windows Search implementation.
Any way to create a program using VB6.0 ?
-
Feb 4th, 2013, 10:30 AM
#2
Re: File Content Search
Once you open your file, use instr() function
-
Feb 4th, 2013, 10:41 AM
#3
Re: File Content Search
Here's a quick example to find the FIRST occurrence of a string within a text file. You can modify to find more occurrences easily.
Code:
Private Sub Command6_Click()
Dim mySearchText As String, x As Integer
Dim fnum As Integer, myFileContents As String
mySearchText = "Fred"
fnum = FreeFile
Open App.Path & "\test.txt" For Input As fnum
myFileContents = Input(LOF(fnum), fnum)
Close fnum
x = InStr(1, myFileContents, mySearchText)
If x > 0 Then
MsgBox Mid(myFileContents, x, Len(mySearchText))
End If
End Sub
-
Feb 4th, 2013, 01:44 PM
#4
Re: File Content Search
If what you are asking is how to search through a lot of files and folders of files then you should be aware that using something like Instr() on each file will be extremely slow compared to windows search which uses indexing.
-
Feb 6th, 2013, 03:18 AM
#5
Re: File Content Search
I'd recommend the file search class from this:
http://leandroascierto.com/blog/clase-para-buscar/
The function it uses to search files seems pretty quick; it uses API and byte arrays... much faster than user regular vb instr code. And it will work with large files.
If you can get past the language barrier, there's some truly amazing code on that site. vbAccelerator level stuff using simple class modules instead of nightmarish user control active x crap.
Code:
Private Function FindWordInFile(ByVal sPath As String, ByVal sWord As String, Optional ByVal bUnicode As Boolean) As Boolean
Dim bArray() As Byte
Dim lRet As Long
Dim hFile As Long
Dim sFind() As Byte
Dim s As String
Dim t As Long
Dim i As Long
Dim FileSize As Currency
Dim tLI As LARGE_INTEGER
Dim LenBuffer As Long
Dim CurPos As Currency
sWord = UCase(sWord)
If bUnicode Then sWord = StrConv(sWord, vbUnicode)
sFind = StrConv(sWord, vbFromUnicode)
hFile = CreateFile(sPath, GENERIC_READ, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, 0, 0)
If hFile <> INVALID_HANDLE_VALUE Then
tLI.LowPart = GetFileSize(hFile, tLI.HighPart)
LenBuffer = &H2800 ' 10 KB
FileSize = LargeIntToCurrency(tLI.LowPart, tLI.HighPart)
If FileSize < UBound(sFind) Then GoTo OutSearch
If LenBuffer > FileSize Then LenBuffer = FileSize
ReDim bArray(LenBuffer - 1)
Do
ReadFile hFile, bArray(0), UBound(bArray) + 1, lRet, 0&
If lRet = 0 Then Exit Do
CurPos = CurPos + lRet
If lRet < LenBuffer Then
ReDim Preserve bArray(lRet)
End If
If InBytes(bArray, sFind) <> -1 Then
FindWordInFile = True
Exit Do
End If
If CurPos = FileSize Then Exit Do
tLI = CurrencyToLargeInt(CurPos - UBound(sFind) + 1)
SetFilePointer hFile, tLI.LowPart, tLI.HighPart, FILE_BEGIN
If c_bCancel Then GoTo OutSearch
Loop
OutSearch:
CloseHandle hFile
End If
End Function
Private Function InBytes(ByRef bvSource() As Byte, ByRef bvMatch() As Byte) As Long
Dim i As Long
Dim j As Long
Dim lChr As Byte
Dim LenMach As Long
LenMach = UBound(bvMatch)
lChr = bvMatch(0)
If LenMach > 0 Then
For i = 0 To UBound(bvSource) - LenMach
If (lChr = aUChars(bvSource(i))) Then
j = LenMach - 1
Do
If bvMatch(j) <> aUChars(bvSource(i + j)) Then GoTo NotEqual
j = j - 1
Loop While j
InBytes = i
Exit Function
End If
NotEqual:
Next
Else
For i = 0 To UBound(bvSource)
If (lChr = aUChars(bvSource(i))) Then
InBytes = i
Exit Function
End If
Next
End If
InBytes = -1
End Function
-
Feb 6th, 2013, 03:22 AM
#6
Thread Starter
Lively Member
Re: File Content Search
Hi to all,
Thanks for the replies.
Sam, yes it is easy to look with InStr within a single file or few bunch of files as well if the file size also comparatively less.
As DataMiser got the point the InStr will be extremely slow in the case of lot of files in a folder or folders.
As Windows Search uses indexing of files and look for a string in to it.
The basic problem of using Windows Search as facing is it can not index the mapped drives path or a URL path.
As in the case of a reporting application where we need to provide a string search in report contents and display the resultant files.
Here if the report creation path is not local or placed on NAS or any other shared folder on other server, then it is quite impossible to use Windows Search for such locations.
So rather than implementing the Windows Search I am thinking to develop some application which follows some Windows Search techniques like creating Indexes or may be anyother way where we can search on any given path.
-
Feb 6th, 2013, 03:27 AM
#7
Thread Starter
Lively Member
Re: File Content Search
#fafalone a great link you given. Thanks to you.
Will trying to work around with it.
Any ideas, for creating the index of files.
-
Feb 6th, 2013, 03:32 AM
#8
Re: File Content Search
I didn't think Windows Search indexed the content of the files? Not sure how you would do that without just copying it.
-
Feb 6th, 2013, 03:39 AM
#9
Thread Starter
Lively Member
Re: File Content Search
Windows Search indexed locations are configured from control panel item "IndexingOptions" if you are using Windows 7 u can see it there. For WinXP systems we need to install Windows Search 4.0
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
|