Re: search plain text files including wildcards



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,

I changed the code to the following:

' Build console command (/i = case insensative, /m = List file
names, /r = search with wildcards)
If InStr(1, List1.List(List1.ListIndex), "*") <> 0 Then
cc = "findstr /i /m /r """ & List1.List(List1.ListIndex) & """
"""
Else
cc = "findstr /i /m """ & List1.List(List1.ListIndex) & """ """
End If


To allow the search with wildcards but it doesn't seem to work.
What am I doing wrong?

Regular Expressions
(Searching for patterns of text)

The FINDSTR syntax notation can use the following metacharacters which
have special meaning either as an operator or delimiter.

. Wildcard: any character

* Repeat: zero or more occurances of previous character or
class

^ Line position: beginning of line
$ Line position: end of line

[class] Character class: any one character in set
[^class] Inverse class: any one character not in set

[x-y] Range: any characters within the specified range

\x Escape: literal use of metacharacter x

\<xyz Word position: beginning of
xyz\> Word position: end of word

Metacharacters are most powerful when they are used together. For
example, the combination of the wildcard character (.) and repeat (*)
character is similar in effect to the filename wildcard (*.*)

..* Match any string of characters

The .* expression may be useful within a larger expression, for
example f.*ing will match any string beginning with F and ending with
ing.

Marco
.



Relevant Pages

  • Re: search plain text files including wildcards
    ... If I'd use the FindStr command. ... Private Declare Sub Sleep Lib "kernel32" ... Dim ff As Long, cnt As Long ... On Error GoTo Sleeper ...
    (microsoft.public.vb.general.discussion)
  • Re: search plain text files including wildcards
    ... If I'd use the FindStr command. ... Private Declare Sub Sleep Lib "kernel32" ... Dim ff As Long, cnt As Long ... On Error GoTo Sleeper ...
    (microsoft.public.vb.general.discussion)
  • RE: Delete still shows the deleted record
    ... I would think, however, that with some modification for ADO, the basic ... Put this in the click event of your "DELETE" command button: ... Public Sub DelCurrentRec ... On Error GoTo Err_Delete_Command_Click ...
    (microsoft.public.access.forms)
  • RE: Delete still shows the deleted record
    ... Put this in the click event of your "DELETE" command button: ... Public Sub DelCurrentRec ... Set rst = Nothing ... On Error GoTo Err_Delete_Command_Click ...
    (microsoft.public.access.forms)
  • Tool to get MCP ODT at Windows CMD prompt
    ... You basically send a series of ODT commands of your choosing, once, or in a loop, and get the output right in the command ... Dim sHostname ... Sub WriteIt ... WriteIt "" ...
    (comp.sys.unisys)