Skip to content

Instantly share code, notes, and snippets.

@TLMcode
Last active January 25, 2017 15:59
Show Gist options
  • Save TLMcode/40e9994b8b767fcce5f41888e44a40b1 to your computer and use it in GitHub Desktop.
Save TLMcode/40e9994b8b767fcce5f41888e44a40b1 to your computer and use it in GitHub Desktop.
Search for string in files
#NoTrayIcon
PrefFile := A_ScriptDir "\prefs.txt"
TCtrlW := 250
if FileExist( PrefFile )
{
FileRead, Prefs, % PrefFile
For Each, Pref in ( StrSplit( Prefs, "~" ), SectionObj := {} )
{
PrefArr := StrSplit( Pref, ">>>" )
SectionObj[ PrefArr[ 1 ] ] := PrefArr[ 2 ]
}
}
Gui Font, s8, Arial
Gui +ToolWindow +AlwaysOnTop
Gui Add, Text,, Find String In Files
Gui Add, Text,, Directory
Gui Add, Edit, % " vDir r1 w" TCtrlW, % SectionObj.Dir
Gui Add, Text,, File Type
Gui Add, Edit, % " vType w" TCtrlW, % SectionObj.Type
Gui Add, Text,, Search Term
Gui Add, Edit, % " vStr w" TCtrlW, % SectionObj.String
Gui Font, s26, Arial
Gui Add, Button, vsBtn Section, Search
GuiControlGet, sBtnPos, Pos, sBtn
Gui Add, Button, % "xp" sBtnPosW+10, Stop
Gui Font, s8, Arial
Gui Add, Text, % "xm vDisp w" TCtrlW, Status:
Gui Show
Return
ButtonSearch:
Gui Submit, NoHide
if ( Dir = "" || Str = "" || Type = "" )
{
MsgBox, 0x10, Whoops!, % ( ( Dir = "" && Str = "" && Type = "" ) ? "Fields" : ( Dir = "" ) ? "Directory" : ( Str = "" ) ? "String" : ( Type = "" ) ? "Type" : "" ) " Cannot Be Blank!`nPlease try again!"
Return
}
FileDelete % PrefFile
FileAppend % "Dir>>>" Dir "~String>>>" Str "~Type>>>" Type, % PrefFile
MatchCount := 0, FileList := ""
; ToDo add recursive search option
Loop, % Dir "\*." Type
{
; ToDo add response for file type not found ( actually don't think I need this as each file will be read 1st anyway )
FileRead, FileContents, % A_LoopFileLongPath
if ( Stop = True )
Break
if ( !MatchCount )
GuiControl,, Disp, Status: Searching...
if InStr( FileContents, Str )
{
MatchCount += 1 ; Matches
GuiControl,, Disp, % "Status: Found " MatchCount " Match" ( MatchCount > 1 ? "es" : "" )
FileList .= MatchCount ": " Str " Found in: " A_LoopFileLongPath "`n"
}
}
; ToDo add checkbox list of files to open
Msgbox % ( FileList = "" ) ? 0x10 : 0x0, % (( FileList = "" ) ? "No" : "Some" ) "thing Found!"
, % ( Stop = True ) ? ( " Search Cancelled!", Stop = False )
: ( FileList = "" ) ? "" Str "" " not found in any " type " files!"
: "String: " Str " Found in " MatchCount " File" ( MatchCount > 1 ? "s" : "" ) "`n" FileList
GuiControl,, Disp, Status:
Return
ButtonStop:
Stop := True
Return
GuiClose:
ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment