#NoTrayIcon #include #include #include #include #include ; #SearchContent# ============================================================================================================== ; Name ..........: SearchContent.AU3 ; Description ...: Research folder and subfolders for specific text inside files ; ; Text is not case sensitive ; ; File filter can be modified and new folders can be added at run time ; Author ........: Nine ; Created .......: 2020-04-08 ; Modified ......: 2023-12-27 ; Remarks .......: List of initial folders and file filter can be changed in the Constants Section below ; Example .......: No ; =============================================================================================================================== Opt("MustDeclareVars", 1) Opt("TrayOnEventMode", 1) Opt("TrayMenuMode", 1) Opt("TrayAutoPause", 0) Global Const $ARRAY_OF_FOLDERS = ["C:\Apps\AutoIt", "C:\Program Files (x86)\AutoIt3\Include"] Global Const $DEFAULT_FILTER = "*.au3" Global Const $SCALING = CalcDPI() Global Const $GUI_TITLE = "Search Content" Global Const $ICON_IDX = 337 Global $hGUI, $idMenuOpen, $idMenuReset, $idMenuCopy, $idTreeFile, $idTVselect SearchContent() Func SearchContent() If Not _Singleton($GUI_TITLE, 1) Then Exit MsgBox($MB_SYSTEMMODAL, "Error", "An instance of this program already exists") $hGUI = GUICreate($GUI_TITLE, 800, 490, 190, 120, $WS_THICKFRAME + $GUI_SS_DEFAULT_GUI) GUISetFont(11 * $SCALING) GUISetIcon("shell32.dll", $ICON_IDX) Local $idSelect = GUICtrlCreateButton("Select Folder", 25, 15, 170, 30) GUICtrlSetResizing(-1, 512) Local $idFolder = GUICtrlCreateCombo("", 205, 18, 580, 25, $CBS_DROPDOWNLIST + $CBS_AUTOHSCROLL) GUICtrlCreateLabel("Filters", 25, 55, 50, 25, $SS_CENTERIMAGE) Local $idFilter = GUICtrlCreateInput($DEFAULT_FILTER, 70, 55, 125, 25) GUICtrlSetTip(-1, 'Multiple filters must be separated by ";"' & @CRLF & _ 'Use "|" to separate 3 sets of filters:' & @CRLF & _ '"Include|Exclude|Exclude_Folders"') GUICtrlSetFont(-1, 10 * $SCALING) GUICtrlCreateLabel("Text to search", 205, 55, 100, 25, $SS_CENTERIMAGE) Local $idText = GUICtrlCreateInput("", 315, 55, 470, 25) $idTreeFile = GUICtrlCreateTreeView(25, 90, 760, 300) GUICtrlSetFont(-1, 9 * $SCALING) Local $idProgress = GUICtrlCreateLabel("", 90, 395, 620, 25, $SS_CENTER + $SS_CENTERIMAGE + $SS_SUNKEN) GUICtrlSetColor(-1, 0xFF0000) Local $idSearch = GUICtrlCreateButton("Search", 90, 430, 120, 45, $BS_DEFPUSHBUTTON) GUICtrlSetImage(-1, "shell32.dll", 134) Local $idOpen = GUICtrlCreateButton("Open", 340, 430, 120, 45) GUICtrlSetImage(-1, "shell32.dll", 46) GUICtrlSetState(-1, $GUI_DISABLE) Local $idReset = GUICtrlCreateButton("Reset", 590, 430, 120, 45) GUICtrlSetImage(-1, "shell32.dll", 16739) GUISetState(@SW_SHOW) Local $idContextmenu = GUICtrlCreateContextMenu($idTreeFile) $idMenuOpen = GUICtrlCreateMenuItem("Open", $idContextmenu) $idMenuReset = GUICtrlCreateMenuItem("Reset", $idContextmenu) $idMenuCopy = GUICtrlCreateMenuItem("Copy Information", $idContextmenu) Local $sFolder, $aSelect[1], $idParent GUICtrlSetData($idFolder, _ArrayToString($ARRAY_OF_FOLDERS), $ARRAY_OF_FOLDERS[0]) GUIRegisterMsg($WM_NOTIFY, WM_NOTIFY) GUIRegisterMsg($WM_GETMINMAXINFO, WM_GETMINMAXINFO) While True Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $GUI_EVENT_MINIMIZE GUISetState(@SW_HIDE) TraySetIcon("shell32.dll", $ICON_IDX) TraySetOnEvent($TRAY_EVENT_PRIMARYUP, GUI_Restore) TraySetOnEvent($TRAY_EVENT_SECONDARYUP, GUI_Exit) TraySetState($TRAY_ICONSTATE_SHOW) TraySetToolTip("Left click to restore" & @CRLF & "Right click to Exit") Case $idSelect $sFolder = FileSelectFolder("Select root folder", @ScriptDir, 0, @ScriptDir, $hGUI) If @error Then ContinueLoop GUICtrlSetData($idFolder, $sFolder, $sFolder) Case $idSearch ReDim $aSelect[1] $aSelect[0] = 0 SearchText($idTreeFile, GUICtrlRead($idFolder), GUICtrlRead($idFilter), GUICtrlRead($idText), $idProgress) Case $idOpen, $idMenuOpen $idTVselect = GUICtrlRead($idTreeFile) OpenFile(GUICtrlRead($idTreeFile, $GUI_READ_EXTENDED), GUICtrlRead($idText)) Case $idFilter If StringStripWS(GUICtrlRead($idFilter), $STR_STRIPLEADING) = "" Then GUICtrlSetData($idFilter, $DEFAULT_FILTER) Case $idReset, $idMenuReset ReDim $aSelect[1] $aSelect[0] = 0 GUICtrlSetData($idText, "") GUICtrlSetData($idProgress, "") _GUICtrlTreeView_DeleteAll($idTreeFile) Case $idMenuCopy ClipInformation($idTreeFile) EndSwitch $idTVselect = GUICtrlRead($idTreeFile) If Not $idTVselect And Not BitAND(GUICtrlGetState($idOpen), $GUI_DISABLE) Then ContinueLoop GUICtrlSetState($idOpen, $GUI_DISABLE) If $idTVselect Then If _GUICtrlTreeView_GetParentHandle($idTreeFile, $idTVselect) Then If Not BitAND(GUICtrlGetState($idOpen), $GUI_DISABLE) Then GUICtrlSetState($idOpen, $GUI_DISABLE) ContinueLoop EndIf If BitAND(GUICtrlGetState($idOpen), $GUI_DISABLE) Then GUICtrlSetState($idOpen, $GUI_ENABLE) For $i = 1 To $aSelect[0] If $idTVselect = $aSelect[$i] Then ContinueLoop 2 Next _ArrayAdd($aSelect, $idTVselect) $aSelect[0] += 1 DisplayLine($hGUI, $idTreeFile, $idTVselect, ControlTreeView($hGUI, "", $idTreeFile, "GetSelected"), GUICtrlRead($idText)) EndIf WEnd EndFunc ;==>SearchContent Func SearchText($idTree, $sFolder, $sFilter, $sText, $idProg) Local $iFound = 0, $iDir = 1, $idItem GUISetCursor($MCID_WAIT, $GUI_CURSOR_OVERRIDE) _GUICtrlTreeView_DeleteAll($idTree) GUICtrlSetData($idProg, "") Local $aDir = _FileListToArrayRec($sFolder, "*", $FLTAR_FOLDERS, $FLTAR_RECUR) If Not @error Then $iDir += $aDir[0] GUICtrlSetData($idProg, "Parsing " & $iDir & " folders...") Local $aFile = _FileListToArrayRec($sFolder, $sFilter, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) If @error Then GUISetCursor() Return MsgBox($MB_SYSTEMMODAL, "Warning", "No file found") EndIf GUICtrlSetData($idProg, "Parsing " & $iDir & " folders - " & $aFile[0] & " files...") For $i = 1 To $aFile[0] If Not $sText Or StringInStr(FileRead($aFile[$i]), $sText) Then $idItem = GUICtrlCreateTreeViewItem($aFile[$i], $idTree) _GUICtrlTreeView_SetChildrenEX($idTree, $idItem) $iFound += 1 EndIf Next GUISetCursor() GUICtrlSetData($idProg, "Parsed " & $aFile[0] & " files in " & $iDir & " folders - " & $iFound & " files found") If Not $iFound Then MsgBox($MB_SYSTEMMODAL, "Error", "Text was not found") EndFunc ;==>SearchText Func _GUICtrlTreeView_SetChildrenEX($hWnd, $idItem, $bFlag = True) Local Static $tItem = DllStructCreate($tagTVITEMEX) $tItem.Mask = BitOR($TVIF_HANDLE, $TVIF_CHILDREN) $tItem.hItem = _GUICtrlTreeView_GetItemHandle($hWnd, $idItem) $tItem.Children = $bFlag Return __GUICtrlTreeView_SetItem($hWnd, $tItem) EndFunc ;==>_GUICtrlTreeView_SetChildrenEX Func DisplayLine($hGUI, $idTree, $idItem, $sItem, $sText) If Not $sText Then Return Local $aLine = FileReadToArray($sItem) For $i = 0 To UBound($aLine) - 1 If StringInStr($aLine[$i], $sText) Then GUICtrlCreateTreeViewItem($aLine[$i], $idItem) Next ControlTreeView($hGUI, "", $idTree, "Expand", $sItem) EndFunc ;==>DisplayLine Func OpenFile($sFile, $sText) Local $sDrive, $sDir, $sFileName, $sExtension _PathSplit($sFile, $sDrive, $sDir, $sFileName, $sExtension) ClipPut($sText) ShellExecute($sFile, "", $sDrive & $sDir, "open") EndFunc ;==>OpenFile Func CalcDPI() Local Const $BASE_SCALING = 96 Local $iDPI = RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics", "AppliedDPI") If @error Then Return 1 Return Round($BASE_SCALING / $iDPI, 2) EndFunc ;==>CalcDPI Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam) Local $aRect, $aPos, $aCtrlPos If GUICtrlGetHandle($idTreeFile) = $tNMHDR.hwndFrom Then If $tNMHDR.Code = $NM_RCLICK Then $aCtrlPos = ControlGetPos($hWnd, "", $idTreeFile) $aRect = _GUICtrlTreeView_DisplayRect($idTreeFile, $idTVselect) $aPos = GUIGetCursorInfo() $aPos[0] -= $aCtrlPos[0] $aPos[1] -= $aCtrlPos[1] If $idTVselect And Not _GUICtrlTreeView_GetParentHandle($idTreeFile, $idTVselect) And _ $aPos[0] >= $aRect[0] And $aPos[0] <= $aRect[2] And $aPos[1] >= $aRect[1] And $aPos[1] <= $aRect[3] Then GUICtrlSetState($idMenuOpen, $GUI_ENABLE) Else GUICtrlSetState($idMenuOpen, $GUI_DISABLE) EndIf EndIf EndIf Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY Func GUI_Restore() TraySetState($TRAY_ICONSTATE_HIDE) GUISetState(@SW_SHOW) WinActivate($hGUI) EndFunc ;==>GUI_Restore Func GUI_Exit() Exit EndFunc ;==>GUI_Exit Func WM_GETMINMAXINFO($hWnd, $Msg, $wParam, $lParam) Local $tMinMaxInfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int", $lParam) DllStructSetData($tMinMaxInfo, 7, 630) ; min width DllStructSetData($tMinMaxInfo, 8, 435) ; min height Return $GUI_RUNDEFMSG EndFunc ;==>WM_GETMINMAXINFO Func ClipInformation($idLV) Local $idTVsel = GUICtrlRead($idLV) If Not $idTVsel Then ClipPut("") Local $sString = _GUICtrlTreeView_GetText($idLV, _GUICtrlTreeView_GetSelection($idLV)) Local $sDrive, $sDir, $sFileName, $sExtension If _GUICtrlTreeView_GetParentHandle($idLV, $idTVsel) Then ClipPut(StringStripWS($sString, $STR_STRIPLEADING + $STR_STRIPTRAILING)) Else _PathSplit($sString, $sDrive, $sDir, $sFileName, $sExtension) ClipPut($sFileName & $sExtension) EndIf EndFunc ;==>ClipInformation