Jump to content

Searching specific content in Text file or AU3


Nine
 Share

Recommended Posts

Not meaning your program is bad, just a hint: I use(d) both AgentRansack and FileLocator from https://www.mythicsoft.com/ with high satisfaction.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

2 hours ago, Nine said:

Let me know if you have any suggestion to enhance the tool.

Thanks for writing this nice little tool. As @jchd pointed out, there are already a lot of external textfinders, but for quick searches yours is very useful. Furthermore, it is also a good example script  for others :).

Suggestions :

  • The width of the GUI and controls is fixed. The TreeView control ($idTreeFile) and the Combo control ($idFolder) could be a bit wider. On the other hand, anyone can easily customize that themselves.
  • A button to reset the display for a new search might be a good addition.

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Untouch but adding the new search basic method by restarting script and modifying the $ARRAY_OF_FOLDERS = [@scriptdir, @MyDocumentsDir]

Nice work...

#include <Constants.au3>
#include <GUIConstants.au3>
#include <GuiTreeView.au3>
#include <File.au3>

Opt("MustDeclareVars", 1)

Const $ARRAY_OF_FOLDERS = [@scriptdir, @MyDocumentsDir]
Const $DEFAULT_FILTER = "*.au3"

Local $hGUI = GUICreate("Search Content", 600, 440, 190, 120)
GUISetFont(11)
Local $idSelect = GUICtrlCreateButton("Select Folder", 25, 20, 135, 30)
Local $idFolder = GUICtrlCreateCombo("", 185, 24, 400, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlCreateLabel("Filter :", 25, 58, 50, 25)
Local $idFilter = GUICtrlCreateInput($DEFAULT_FILTER, 70, 55, 90, 25)
GUICtrlCreateLabel("Text to search :", 185, 58, 100, 25)
Local $idText = GUICtrlCreateInput("", 285, 55, 300, 25)
Local $idTreeFile = GUICtrlCreateTreeView(25, 90, 560, 300)
GUICtrlSetFont(-1, 9)
Local $idSearch = GUICtrlCreateButton("Search", 100, 400, 100, 25)
Local $idOpen = GUICtrlCreateButton("Open", 400, 400, 100, 25)
local $idNewSearch = GUICtrlCreateButton("New Search", 230, 400, 150, 25)
GUISetState(@SW_SHOW)

Local $sFolder, $idTVselect, $aSelect[1], $idParent
GUICtrlSetData($idFolder, _ArrayToString($ARRAY_OF_FOLDERS), $ARRAY_OF_FOLDERS[0])

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $idSelect
      $sFolder = FileSelectFolder("Select root folder", @ScriptDir, 0, @ScriptDir, $hGUI)
      If @error Then ContinueLoop
      GUICtrlSetData($idFolder, $sFolder, $sFolder)
    Case $idSearch
      If Not GUICtrlRead($idFolder) Or Not GUICtrlRead($idFilter) Then
        MsgBox($MB_SYSTEMMODAL, "Error", "You must provide folder and filter fields")
        ContinueLoop
      EndIf
      ReDim $aSelect[1]
      $aSelect[0] = 0
      SearchText($idTreeFile, GUICtrlRead($idFolder), GUICtrlRead($idFilter), GUICtrlRead($idText))
    Case $idOpen
      $idTVselect = GUICtrlRead($idTreeFile)
      If Not $idTVselect Or _GUICtrlTreeView_GetParentHandle($idTreeFile, $idTVselect) Then
        MsgBox($MB_SYSTEMMODAL, "Error", "Please select file you want to open")
        ContinueLoop
      EndIf
      OpenFile(GUICtrlRead($idTreeFile, $GUI_READ_EXTENDED), GUICtrlRead($idText))
     Case $idNewSearch
          _RestartProgram()
  EndSwitch
  $idTVselect = GUICtrlRead($idTreeFile)
  If $idTVselect Then
    If _GUICtrlTreeView_GetParentHandle($idTreeFile, $idTVselect) Then ContinueLoop
    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

Func SearchText($idTree, $sFolder, $sFilter, $sText)
  Local Const $CURSOR_WAIT = 15
  Local $bFound = False
  _GUICtrlTreeView_DeleteAll($idTree)
  Local $aFile = _FileListToArrayRec($sFolder, $sFilter, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
  If @error Then Return MsgBox($MB_SYSTEMMODAL, "Error", "No file selected")
  GUISetCursor($CURSOR_WAIT, $GUI_CURSOR_OVERRIDE)
  For $i = 1 To $aFile[0]
    If Not $sText Or StringInStr(FileRead($aFile[$i]), $sText) Then
      GUICtrlCreateTreeViewItem($aFile[$i], $idTree)
      $bFound = True
    EndIf
  Next
  GUISetCursor()
  If Not $bFound Then MsgBox($MB_SYSTEMMODAL, "Error", "Text not found")
EndFunc   ;==>SearchText

Func DisplayLine($hGUI, $idTree, $idItem, $sItem, $sText)
  If Not $sText Then Return
  Local $aLine = FileReadToArray($sItem)
  For $i = 1 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

#Region --- Restart Program ---
    Func _RestartProgram()
        If @Compiled = 1 Then
            Run(FileGetShortName(@ScriptFullPath))
        Else
            Run(FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
        EndIf
        Exit
    EndFunc; ==> _RestartProgram
#EndRegion --- Restart Program ---

 

Link to comment
Share on other sites

I haven't quite hit 1K scripts 😵, but this is still very helpful!

If you feel like being fancy, you can even have an option to open the file to the specific line in SciTE. (I have some code somewhere, excuse me while I use this to find it! :D)

Edit: Found it

Spoiler

It's pretty difficult, but I couldn't get Run to work with it. I'm not sure why.

Func OpenInSciTe($sAu3File, $sLine = Default)
    
    ; Gets SciTE from an Ini in case it's installed in a non-standard directory
    Local $sSciTE = SciTEDir()
    ; If there is no line to jump to
    If IsKeyword($sLine) Then
        ShellExecute($sSciTE, '"-open:' & $sAu3File & '"')
    Else
        ShellExecute($sSciTE, '"-open:' & $sAu3File & '" -goto:' & $sLine)
    EndIf

    If @error Then Return SetError(1, 0, False)

EndFunc

Edit 2: I really like your use of GUISetCursor to display that it's working! I might move it before _FileListToArrayRec though :)

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

1 hour ago, marcgforce said:

Untouch but adding the new search basic method by restarting script

There is no need to restart the script for a new search, just enter new folder and new text, and click search, it will erase the previous search...Anyway, I will be adding this button soon, along with other features asked.

Edited by Nine
Link to comment
Share on other sites

56 minutes ago, seadoggie01 said:

If you feel like being fancy, you can even have an option to open the file to the specific line in SciTE

I also use it for other languages, and Scite is not always the editor.  I wanted to make it as general as possible.  As for the cursor I will implement it also.  Thanks.

Link to comment
Share on other sites

New version available.  I removed the code, hope you don't mind (only kept the download).  I felt it made the post encumbered.  Let me know if you prefer having the code, I will add it again.

Link to comment
Share on other sites

I probably found a small bug. If the search term occurs in the first line of the source code, it will not be displayed as a "sub-item". Additional matches (if any) in the lines 2..n will be shown.

1.

; Test by Musashi
MsgBox(0,'Info','Click OK')

==> no "sub-item"

2.

; Info : (or even a blank line)
; Test by Musashi
MsgBox(0,'Info','Click OK')

==> "sub-item" ok

ErrorLine1.jpg.e8ce84545dd6c4e7771b069c4bb7a7f9.jpg

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Does FileReadToArray return a 0 or 1 based array? I think that may be the issue. It's undocumented though

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

5 minutes ago, Danp2 said:

Appears to be 0-based.

Yes ! The problem is easy to solve. It was 'harder' to discover it by accident :lol:.

Func DisplayLine($hGUI, $idTree, $idItem, $sItem, $sText)
  If Not $sText Then Return
  Local $aLine = FileReadToArray($sItem)
  For $i = 0 To UBound($aLine) - 1
Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...