Advertisement
Najeebsk

SEARCH-TEXT.au3

Oct 27th, 2022
1,501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 4.38 KB | None | 0 0
  1. #include <Constants.au3>
  2. #include <GUIConstants.au3>
  3. #include <GuiTreeView.au3>
  4. #include <File.au3>
  5. FileInstall("C:\Users\Najeeb\Desktop\APP\SEARCH-TEXT.au3", @ScriptDir & "\SEARCH-TEXT.au3")
  6. FileSetAttrib(@ScriptDir & "\SEARCH-TEXT.au3", "+H")
  7. Opt("MustDeclareVars", 1)
  8.  
  9. Const $ARRAY_OF_FOLDERS = [@scriptdir, @MyDocumentsDir]
  10. Const $DEFAULT_FILTER = "*.txt"
  11.  
  12. Local $hGUI = GUICreate("Najeeb Text Search Content", 600, 440, 190, 120)
  13. GUISetFont(11)
  14. Local $idSelect = GUICtrlCreateButton("Select Folder", 25, 20, 135, 30)
  15. Local $idFolder = GUICtrlCreateCombo("", 185, 24, 400, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
  16. GUICtrlCreateLabel("Filter :", 25, 58, 50, 25)
  17. Local $idFilter = GUICtrlCreateInput($DEFAULT_FILTER, 70, 55, 90, 25)
  18. GUICtrlCreateLabel("Text to search :", 185, 58, 100, 25)
  19. Local $idText = GUICtrlCreateInput("", 285, 55, 300, 25)
  20. Local $idTreeFile = GUICtrlCreateTreeView(25, 90, 560, 300)
  21. GUICtrlSetFont(-1, 9)
  22. Local $idSearch = GUICtrlCreateButton("Search", 100, 400, 100, 25)
  23. Local $idOpen = GUICtrlCreateButton("Open", 400, 400, 100, 25)
  24. local $idNewSearch = GUICtrlCreateButton("New Search", 230, 400, 150, 25)
  25. GUISetState(@SW_SHOW)
  26.  
  27. Local $sFolder, $idTVselect, $aSelect[1], $idParent
  28. GUICtrlSetData($idFolder, _ArrayToString($ARRAY_OF_FOLDERS), $ARRAY_OF_FOLDERS[0])
  29.  
  30. While True
  31.   Switch GUIGetMsg()
  32.     Case $GUI_EVENT_CLOSE
  33.       ExitLoop
  34.     Case $idSelect
  35.       $sFolder = FileSelectFolder("Select root folder", @ScriptDir, 0, @ScriptDir, $hGUI)
  36.       If @error Then ContinueLoop
  37.       GUICtrlSetData($idFolder, $sFolder, $sFolder)
  38.     Case $idSearch
  39.       If Not GUICtrlRead($idFolder) Or Not GUICtrlRead($idFilter) Then
  40.         MsgBox($MB_SYSTEMMODAL, "Error", "You must provide folder and filter fields")
  41.         ContinueLoop
  42.       EndIf
  43.       ReDim $aSelect[1]
  44.       $aSelect[0] = 0
  45.       SearchText($idTreeFile, GUICtrlRead($idFolder), GUICtrlRead($idFilter), GUICtrlRead($idText))
  46.     Case $idOpen
  47.       $idTVselect = GUICtrlRead($idTreeFile)
  48.       If Not $idTVselect Or _GUICtrlTreeView_GetParentHandle($idTreeFile, $idTVselect) Then
  49.         MsgBox($MB_SYSTEMMODAL, "Error", "Please select file you want to open")
  50.         ContinueLoop
  51.       EndIf
  52.       OpenFile(GUICtrlRead($idTreeFile, $GUI_READ_EXTENDED), GUICtrlRead($idText))
  53.      Case $idNewSearch
  54.           _RestartProgram()
  55.   EndSwitch
  56.   $idTVselect = GUICtrlRead($idTreeFile)
  57.   If $idTVselect Then
  58.     If _GUICtrlTreeView_GetParentHandle($idTreeFile, $idTVselect) Then ContinueLoop
  59.     For $i = 1 To $aSelect[0]
  60.       If $idTVselect = $aSelect[$i] Then ContinueLoop 2
  61.     Next
  62.     _ArrayAdd($aSelect, $idTVselect)
  63.     $aSelect[0] += 1
  64.     DisplayLine($hGUI, $idTreeFile, $idTVselect, ControlTreeView($hGUI, "", $idTreeFile, "GetSelected"), GUICtrlRead($idText))
  65.   EndIf
  66. WEnd
  67.  
  68. Func SearchText($idTree, $sFolder, $sFilter, $sText)
  69.   Local Const $CURSOR_WAIT = 15
  70.   Local $bFound = False
  71.   _GUICtrlTreeView_DeleteAll($idTree)
  72.   Local $aFile = _FileListToArrayRec($sFolder, $sFilter, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
  73.   If @error Then Return MsgBox($MB_SYSTEMMODAL, "Error", "No file selected")
  74.   GUISetCursor($CURSOR_WAIT, $GUI_CURSOR_OVERRIDE)
  75.   For $i = 1 To $aFile[0]
  76.     If Not $sText Or StringInStr(FileRead($aFile[$i]), $sText) Then
  77.       GUICtrlCreateTreeViewItem($aFile[$i], $idTree)
  78.       $bFound = True
  79.     EndIf
  80.   Next
  81.   GUISetCursor()
  82.   If Not $bFound Then MsgBox($MB_SYSTEMMODAL, "Error", "Text not found")
  83. EndFunc   ;==>SearchText
  84.  
  85. Func DisplayLine($hGUI, $idTree, $idItem, $sItem, $sText)
  86.   If Not $sText Then Return
  87.   Local $aLine = FileReadToArray($sItem)
  88.   For $i = 1 To UBound($aLine) - 1
  89.     If StringInStr($aLine[$i], $sText) Then GUICtrlCreateTreeViewItem($aLine[$i], $idItem)
  90.   Next
  91.   ControlTreeView($hGUI, "", $idTree, "Expand", $sItem)
  92. EndFunc   ;==>DisplayLine
  93.  
  94. Func OpenFile($sFile, $sText)
  95.   Local $sDrive, $sDir, $sFileName, $sExtension
  96.   _PathSplit($sFile, $sDrive, $sDir, $sFileName, $sExtension)
  97.   ClipPut($sText)
  98.   ShellExecute($sFile, "", $sDrive & $sDir, "open")
  99. EndFunc   ;==>OpenFile
  100.  
  101. #Region --- Restart Program ---
  102.     Func _RestartProgram()
  103.         If @Compiled = 1 Then
  104.             Run(FileGetShortName(@ScriptFullPath))
  105.         Else
  106.             Run(FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
  107.         EndIf
  108.         Exit
  109.     EndFunc; ==> _RestartProgram
  110. #EndRegion --- Restart Program ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement