Re: search plain text files including wildcards

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 10 apr, 16:08, "Larry Serflaten" <serfla...@xxxxxxxxxxxxxx> wrote:
"Co" <vonclausow...@xxxxxxxxx> wrote

Well, If you limit your search to VB code, then you'll have to
type that code in....

Or get someone else to do it... ;-)

If I'd use the FindStr command.
How could I call it from VB?
Could you give me an example to work on?

OK, here is one way to do that task, using one listbox to allow you
to select words to search for, and a second listbox to show the
results. So, Add two listboxes to a new form and paste in the code
below. Change the constants to work with your file system....

HTH
LFS

Option Explicit
Const TempWorkDir = "D:\Temp\" ' Working directory for result file
Const SearchDir = "D:\Temp\" ' Directory to search
Const FileType = "*.txt" ' File type to include in search

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Load()
List1.Move 120, 120, 1695, 3960
List2.Move 1920, 120, 3000, 3960
' Add words you want to search for...
List1.AddItem "Many"
List1.AddItem "Things"
List1.AddItem "Are"
List1.AddItem "Looking"
List1.AddItem "Brighter"
List1.AddItem "Now"
End Sub

Private Sub List1_Click()
' Click a word in the list to see filenames that contain
' that word. Its a Reg. Ex. search similar to "*word*"
Dim cc As String
Dim rst As String
Dim ff As Long, cnt As Long

' Initialize
List2.Clear
ff = FreeFile

' Assign and kill result file
rst = TempWorkDir & "FS_Result.txt"
On Error Resume Next
Kill rst
On Error GoTo 0

' Build console command (/i = case insensative, /m = List file names)
cc = "findstr /i /m """ & List1.List(List1.ListIndex) & """ """
cc = cc & SearchDir & FileType & """ "
cc = cc & "> " & rst
Debug.Print cc ' see the command

' Run command
Shell Environ("COMSPEC") & " /c " & cc, vbHide
DoEvents

' Waiting for file access
On Error GoTo Sleeper
Sleeper:
' Limit recursion
cnt = cnt + 1
If cnt > 100 Then
MsgBox "Too many to list...."
Exit Sub
End If

DoEvents
Sleep 50
' Try to lock result file (error causes recursion to Sleeper)
Open rst For Input Access Read Lock Write As ff

' File locked, get data
On Error GoTo 0
Do While Not EOF(ff)
Line Input #ff, cc
List2.AddItem cc
Loop
Close ff

End Sub

Larry,

indeed that makes things a lot clearer.
Can it also use wildcards like ? and * ?

Marco
.


Quantcast