递归搜索文件中的字符串

Search a file recursively for string

我正在使用一些 vbscript 在目录中搜索字符串。现在我需要我的脚本在每个文件和 return 行号或其他一些可识别信息中搜索字符串的多个实例。我当前的 vbscript 是:-

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files
    For Each thing in Fls
        Set contents = thing.OpenAsTextStream
        If err.number = 0 then
            Test = Instr(contents.readall, searchterm) 
            If Isnull(test) = false then If Test > 0 then ObjOutFile.WriteLine thing.path 
            demo.innerHtml = demo.innerHtml & thing.path & "<br>"
        Else
            err.clear
        End If
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next
End Sub

我需要遍历每个文件的行以获取包含字符串的行号。

读取每一行并搜索每一行。还记得以前的搜索和目录。

您可以解决两个问题。

  1. 如果通过 exe 文件,它现在会遇到 500 万条语句超时消息 esp。

  2. 找不到 Unicode 文本。

第三次写程序了

<HTML> 
<HEAD><TITLE>Simple Validation</TITLE> 
<SCRIPT LANGUAGE="VBScript">
Dim Dirname
Dim Searchterm
Dim FSO
Dim objOutFile


Sub Browse
    On Error Resume Next
    Set bffShell = CreateObject("Shell.Application")
    Set bff = bffShell.BrowseForFolder(0, "Select the My Documents folder", 9)
    If Err.number<>0 Then
        MsgBox "Error Setting up Browse for Folder"
    Else
        A = bff.ParentFolder.ParseName(bff.Title).Path
        If err.number=424 then err.clear
                tb2.value = A
    End If
End Sub

Sub Search
    On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.RegWrite "HKCU\Software\Whosebug\VBS\Searchterm", tb1.value
    WshShell.RegWrite "HKCU\Software\Whosebug\VBS\Directory", tb2.value
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objOutFile = fso.CreateTextFile("results.txt",True)
    Dirname = tb2.value
    Searchterm = tb1.value
    ProcessFolder DirName
End Sub

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files
    For Each thing in Fls
        Set contents = thing.OpenAsTextStream
        If err.number = 0 then
        Linenum = 0
        Do Until contents.AtEndOfStream
            line = contents.readline
            Linenum = Linenum + 1
                Test = Instr(line, searchterm) 
                If Isnull(test) = false then If Test > 0 then ObjOutFile.WriteLine LineNum & "  " & thing.path
        Loop
        Else
            err.clear
        End If
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub

Sub Init
    On Error Resume Next
    Set WshShell = CreateObject("WScript.Shell")
    tb1.value = WshShell.RegRead("HKCU\Software\Whosebug\VBS\Searchterm")
    tb2.value = WshShell.RegRead("HKCU\Software\Whosebug\VBS\Directory")
End Sub

</script>
</head>
<body Onload=Init>
<p><INPUT Name=tb1 TYPE=Text Value="Search">
<p><INPUT Name=tb2 TYPE=Text Value="Folder"> <INPUT NAME="Browse" TYPE="BUTTON" VALUE="Browse" OnClick=Browse>
<p><INPUT NAME="Search" TYPE="BUTTON" VALUE="Search" OnClick=Search>
</body>
</html>