通过输入框在所有文件中搜索字符串

search string in all files by input box

此代码来自此处:https://autohotkey.com/board/topic/94861-search-for-a-string-in-all-files-in-a-folder/

此代码用于 "search for a string in files"

File := "*.txt"             ;can include directory -- * is wildcard
StringCheck := "string"     ;replace with search string
FileHit := ""               ;empty

Loop, %File%, , 1
{
   FileRead, FileCheck, %A_LoopFileLongPath%
   IfInString, FileCheck, %StringCheck%
      FileHit%A_Index% := A_LoopFileLongPath
}
Loop, 100
{
   If (FileHit%A_Index% <> "")
      FileHit .= FileHit%A_Index% . "`n"
}
MsgBox, % FileHit

我的问题是: 代码是否有可能从输入框中获取字符串,以及用户从对话框中获取的路径

如果有人能告诉我如何做到这一点,我将不胜感激!

尝试在您的脚本中添加以下内容:

File := "*.txt"             ;can include directory -- * is wildcard
StringCheck := ""           ;replace with search string
FileHit := ""               ;empty

InputBox, Directory, Enter the searched directory, Leave empty to use the current directory. Subfolders will be also searched., , 300, 150
if ErrorLevel
{
    MsgBox, Query canceled.
    Exit
}

Directory := RegExReplace(Directory, "\$") ; Removes the leading backslash, if present.

If Directory
{
    If(!InStr(FileExist(Directory), "D"))
    {
        msgbox Invalid directory
        Exit
    }
    StringRight, DirectoryEndingChar, Directory, 1
    If(DirectoryEndingChar != "\")
        Directory .= "\"
}

InputBox, StringCheck, Enter string to search, The search string is not case sensitive., , 300, 150

if ErrorLevel
{
    MsgBox, Query canceled.
    Exit
}

Loop, %Directory%%File%, , 1
{
   FileRead, FileCheck, %A_LoopFileLongPath%
   IfInString, FileCheck, %StringCheck%
      FileHit%A_Index% := A_LoopFileLongPath
}
Loop, 100
{
   If (FileHit%A_Index% <> "")
      FileHit .= FileHit%A_Index% . "`n"
}

If FileHit
    MsgBox, % FileHit
Else
    MsgBox, No match found.