需要帮助来更改脚本

时间:2018-06-06 20:37:59

标签: batch-file

我需要帮助才能通过此链接找到Hackoo更改脚本。

batch file that searches for strings in a text file inside all files in folders/subfolders

脚本很好,但在日志而不是搜索文本中,它会在括号中生成数字。

例如,我正在寻找文本textlist.txt中的文本“My_Cat”。该脚本找到了一个文件,其中包含“My_Cat”,但在搜索结束后,它将数字放在括号中,而不是“My_Cat”。创建日志时同样的事情。真?号码会搜索文字吗? 0_0

简而言之,我需要改为“[1] - Test_Dog.xml - ”D:\ Files \ Test_Dog.xml“是它”[My_Cat] - Test_Dog - “D:\ Files \ Test_Dog。 xml“的

1 个答案:

答案 0 :(得分:0)

; AutoIt Version:   3.3.10.2 or later.
; Author:           Michael Heath
; Script Function:  Searches files for strings in recursive mode using findstr.

#pragma compile(Out, 'searcher.exe')
#pragma compile(FileVersion, 3.3.10.2)
#pragma compile(ProductVersion, 1.1.0.0)
#pragma compile(FileDescription, 'Searcher')
#pragma compile(ProductName, 'Searcher')
#pragma compile(Console, False)

#region - Generated Constants
; If Std includes added cause error with these,
; then remove any of these constants to fix.
Global Const $GUI_EVENT_CLOSE = -3
Global Const $GUI_CHECKED = 1
Global Const $GUI_UNCHECKED = 4
Global Const $GUI_ENABLE = 64
Global Const $GUI_DISABLE = 128
#endregion

; Get the search executable for main use.
$sSearchExe = _Get_SearchExe()

#region - CmdlineSwitch
If $CMDLINE[0] Then
    ; Patterns file
    $sStringsFile = ''
    ; i.e *.txt
    $sFilePatterns = ''
    ; Directory to search from.
    $sSearchPath = ''
    ; Output with filenames and search strings.
    $bWithNoStrings = False

    For $i = 1 To $CMDLINE[0]
        Switch $CMDLINE[$i]
            Case '/?'
                MsgBox(0x40000, 'Searcher Help', _
                 'Searches files for strings in recursive mode using findstr.' & @CRLF & @CRLF & _
                 'Syntax: ' & @ScriptName & ' search_path patternfile filepattern [[filepattern] ... ] [/m]' & @CRLF & _
                 @CRLF & _
                 'Arguments are:' & @CRLF _
                 & @CRLF & 'search_path' _
                 & @CRLF & @TAB & 'Directory path to search from.' _
                 & @CRLF & 'patternfile' _
                 & @CRLF & @TAB & 'Gets search strings from the specified file.' _
                 & @CRLF & 'filepattern' _
                 & @CRLF & @TAB & 'File to search for i.e. *.txt.' _
                 & @CRLF & '/m' _
                 & @CRLF & @TAB & 'Output files and no strings.' _
                 & @CRLF & @TAB & 'Only works with findstr.' _
                 & @CRLF & @CRLF & _
                 'No arguments passed shows the Gui.' & @CRLF & @CRLF & _
                 'Arguments passed may require a command to get stdout i.e a CMD for loop. ' & _
                 'Or pipe to more i.e. command|more. If compiled as console then ' & _
                 'getting the stdout should be like using a console executable.' & @CRLF & @CRLF & _
                 'An ini file can be used named "searcher.ini" in the program directory i.e.' & @CRLF & @CRLF & _
                 ' [main]' & @CRLF & _
                 ' SearchExe=findstr' & @CRLF & @CRLF & _
                 'You can use "SearchExe=strings" or "SearchExe=strings64" ' & _
                 'which are available from Windows Sysinternals.' _
                )
                Exit

            Case Else
                If $CMDLINE[$i] == '/m' Then
                    $bWithNoStrings = True
                ElseIf $sSearchPath == '' Then
                    $sSearchPath = $CMDLINE[$i]
                ElseIf $sStringsFile == '' Then
                    $sStringsFile = $CMDLINE[$i]
                Else
                    $sFilePatterns &= '"' & $CMDLINE[$i] & '" '
                EndIf
        EndSwitch
    Next

    $sFilePatterns = StringTrimRight($sFilePatterns, 1)

    If $sFilePatterns == '' Or $sSearchPath == '' Or $sStringsFile == '' Then
        ConsoleWriteError('Incorrect arguments passed' & @CRLF)
        Exit 1
    EndIf

    $sStdout = _Run_Search($sFilePatterns, $sSearchPath, $sStringsFile, $bWithNoStrings)

    ; Trim ends of whitespace and output to stdout.
    $sStdout = StringStripWS($sStdout, 3)
    ConsoleWrite($sStdout & @CRLF)
    Exit
EndIf
#endregion

$iMainGui = GUICreate('Searcher', 640, 580)
GUICtrlCreateLabel('Path to search', 10, 10)
$iSearchPath = GUICtrlCreateInput('', 10, 30, 500)
GUICtrlCreateLabel('File patterns', 10, 60)
$iFilePatterns = GUICtrlCreateInput('', 10, 80, 500)
GUICtrlCreateLabel('Strings to find', 10, 110)
$iSearchStrings = GUICtrlCreateEdit('', 10, 130, 500, 200)
GUICtrlCreateLabel('Result', 10, 340)
$iResult = GUICtrlCreateEdit('', 10, 360, 500, 200)
$iButtonSearchPath = GUICtrlCreateButton('...', 520, 27, 25)
$iButtonFilePattern = GUICtrlCreateButton('...', 520, 130, 25)
$iComboSearchExe = GUICtrlCreateCombo('', 520, 360, 100, Default, 0x3); $CBS_DROPDOWNLIST
GUICtrlSetData($iComboSearchExe, _Get_SearchExe(True), $sSearchExe)
$iCheckboxWithStrings = GUICtrlCreateCheckbox('Output no strings', 520, 460)
If $sSearchExe <> 'findstr' Then
    GUICtrlSetState($iCheckboxWithStrings, $GUI_DISABLE)
EndIf
$iButtonSearch = GUICtrlCreateButton('Search', 520, 500, 100, 60)
GUISetState()

GUICtrlSetTip($iFilePatterns, 'i.e. *.txt')
GUICtrlSetTip($iButtonFilePattern, 'Select pattern file to read')
GUICtrlSetTip($iButtonSearchPath, 'Select folder to search')
GUICtrlSetTip($iComboSearchExe, 'Select main search executable')

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

        Case $iButtonFilePattern
            ; Backup current workdir as FileOpenDialog changes it.
            $sSaveDir = @WorkingDir

            ; Dialog option 1 = File must exist.
            $sSelectedFile = FileOpenDialog('Pattern file containing search strings', _
             '', 'Text files (*.txt)|All files (*.*)', 1, '', $iMainGui _
            )

            If @error Then ContinueLoop
            GUICtrlSetData($iSearchStrings, FileRead($sSelectedFile))
            FileChangeDir($sSaveDir)

        Case $iButtonSearch
            ; Read ctrls and trim ends of whitespace.
            $sFilePatterns = StringStripWS(GUICtrlRead($iFilePatterns), 3)
            $sSearchPath = StringStripWS(GUICtrlRead($iSearchPath), 3)
            $sSearchStrings = StringStripWS(GUICtrlRead($iSearchStrings), 3)

            If $sFilePatterns == '' Then ContinueLoop
            If $sSearchPath == '' Then ContinueLoop
            If $sSearchStrings == '' Then ContinueLoop

            $sStringsFile = _Save_PatternFile($sSearchStrings)
            If @error Then
                MsgBox(0x30, @ScriptName, 'Write temp patterns file error', 0, $iMainGui)
                ContinueLoop
            EndIf

            If GUICtrlRead($iCheckboxWithStrings) = $GUI_CHECKED Then
                $bWithNoStrings = True
            Else
                $bWithNoStrings = False
            EndIf

            $sStdout = _Run_Search($sFilePatterns, $sSearchPath, $sStringsFile, $bWithNoStrings)

            GUICtrlSetData($iResult, $sStdout)

        Case $iButtonSearchPath
            $sSelectedDir = FileSelectFolder('Choose search directory.', _
             '', 0, '', $iMainGui _
            )

            If @error Then ContinueLoop
            GUICtrlSetData($iSearchPath, $sSelectedDir)

        Case $iComboSearchExe
            $sSearchExe = GUICtrlRead($iComboSearchExe)

            Switch $sSearchExe
                Case 'findstr'
                    GUICtrlSetState($iCheckboxWithStrings, $GUI_ENABLE)

                Case Else
                    GUICtrlSetState($iCheckboxWithStrings, $GUI_UNCHECKED)
                    GUICtrlSetState($iCheckboxWithStrings, $GUI_DISABLE)
            EndSwitch
    EndSwitch
WEnd

Exit


Func _Get_SearchExe($bGetComboString = False)
    Local $sComboString, $sSearchExe

    ; Vaild entries to select from.
    If @OSArch = 'X86' Then
        Local $aSearchExe[] = ['findstr', 'strings']
    Else
        Local $aSearchExe[] = ['findstr', 'strings', 'strings64']
    EndIf

    If $bGetComboString Then
        For $sItem in $aSearchExe
            $sComboString &= $sItem & '|'
        Next

        $sComboString = StringTrimRight($sComboString, 1)

        Return $sComboString
    EndIf

    $sSearchExe = IniRead(@ScriptDir & '\searcher.ini', 'Main', 'SearchExe', 'findstr')

    ; Trim .exe from right side.
    If StringRegExp($sSearchExe, '(?i).exe\Z') Then
        $sSearchExe = StringTrimRight($sSearchExe, 4)
    EndIf

    ; Return valid search exe.
    For $sItem in $aSearchExe
        If $sSearchExe = $sItem Then Return $sSearchExe
    Next

    ; Else return default search exe.
    Return 'findstr'
EndFunc


Func _Save_PatternFile($sSearchStrings)
    Local $hWrite, $sStringsFile

    ; Get unique filename for patterns file.
    $sStringsFile = @TempDir & '\searcher1000.tmp'

    For $i1 = 1000 To 10000
        If Not FileExists(@TempDir & '\searcher' & $i1 & '.tmp') Then
            $sStringsFile = @TempDir & '\searcher' & $i1 & '.tmp'
            ExitLoop
        EndIf
    Next

    ; Write file for findstr to get strings from using utf-8 and erase mode.
    $hWrite = FileOpen($sStringsFile, 0x102)
    If $hWrite = -1 Then Return SetError(1, 0, '')
    FileWrite($hWrite, $sSearchStrings & @CRLF)
    FileClose($hWrite)

    Return $sStringsFile
EndFunc


Func _Run_Search($sFilePatterns, $sSearchPath, $sStringsFile = @TempDir & '\searcher1000.tmp', $bWithNoStrings = False)
    Local $iPid, $sCommand, $sStdout
    Dim $sSearchExe

    If Not $sSearchExe Then $sSearchExe = 'findstr'

    ; Build command.
    If StringLeft($sSearchExe, 7) = 'strings' Then
        $sCommand = 'cmd /c ' & $sSearchExe & ' -nobanner -s ' & $sFilePatterns & _
         '|' & 'findstr /l /i /g:"' & $sStringsFile & '"'
    Else
        $sCommand = 'findstr /l /i /s '

        If $bWithNoStrings Then $sCommand &= '/m '

        $sCommand &= '/g:"' & $sStringsFile & '" ' & $sFilePatterns
    EndIf

    ; Run findstr and get stdout (opt_flag = 2).
    $iPid = Run($sCommand, $sSearchPath, @SW_HIDE, 2)

    Do
        Sleep(10)
        $sStdout &= StdoutRead($iPid)
    Until @error

    ; Cleanup tmp file.
    if StringRegExp($sStringsFile, '(?i)searcher\d{4,}\.tmp\Z') Then
        FileDelete($sStringsFile)
    EndIf

    Return $sStdout
EndFunc

这是AutoIt v3代码。它使用findstr进行递归搜索。 如果没有传递任何参数,那么它会显示Gui。 如果参数传递并输出到stdout和stderr,则不显示Gui。

编译并测试为Gui。可能编译为崔可能 尽管你可以在Gui时获得一个控制台窗口,但是更好地使用CLI 没有参数传递时打开。

支持CLI参数,因此可以在CMD脚本或类似脚本中使用。 使用参数/?查看显示语法,参数的消息框 可用,描述和其他有用的信息。

CLI语法为searcher search_path patternfile filepattern [[filepattern] ... ] [/m]

创建完全不像Hackoo的代码那样 懒得读它。也许类似但不同的语言 使用它使它不同使用。 如果缺少某个功能,请告诉我,我可能会对其进行更新。

注意:我可能不会列出所有更新的项目。预期的主要项目。

使用复选框控件和参数更新,以允许输出无字符串。 即filepath:found_string。这是findstr输出的格式。

更新为使用不含BOM的UTF-8用于findstr读取的模式文件。

已更新为使用findstrstringsstrings64Strings的主页。 将流可执行文件放在PATH中可访问的目录中。