按输入框搜索所有文件中的字符串

时间:2017-02-20 19:01:52

标签: autohotkey

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

此代码用于"在文件"

中搜索字符串
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

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

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

1 个答案:

答案 0 :(得分:1)

尝试添加以下脚本:

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.