asad41163
asad41163

Reputation: 49

search string in all files by input box

This code from here: https://autohotkey.com/board/topic/94861-search-for-a-string-in-all-files-in-a-folder/

This code for "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

my question is: Is it possible the code take the string from inputbox, and the path from dialog box by the user

If someone could show me how to do this, I would be very grateful!

Upvotes: 1

Views: 469

Answers (1)

Raul
Raul

Reputation: 3141

Try following addition to your script:

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.

Upvotes: 1

Related Questions