Jump to content

Searching specific content in Text file or AU3


Recommended Posts

On 4/8/2020 at 4:11 PM, Nine said:

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

why I'm sure i 'm forgetting something :huh2:

Made the example will update it from Here

Note that  you can easily adjust this kind of gui you have with "_GUICtrlTuner_Limitless.au3(needs an update ..)  ():  takes a little practice to get used to handling it though (remove any vars from guicreate lines before running it otherwise the control wont show ..

adding to the top of the gui script to make changes 

;~ #include "_GUICtrlTuner_Limitless.au3"
;~ _EnumGUICtrl(@ScriptFullPath)

 

Edited by Deye
Link to post
Share on other sites
Link to post
Share on other sites

What was the action you were doing when this happens ?  Never seen that error before.  Unless, I can replicate the error, I will not be able to solve it.  And at the moment, you are the only one that have seen it.  And as you can note, my script doesn't have that line, so it must come from a UDF somewhere.  Do you have an old version of AutoIt ?

Link to post
Share on other sites

Stress test : Search in a folder with 498 subdirectories (10.198 files in total, with 1.351 .au3 files) ==> NO ERRORS

@Nine BTW : This might be a small but useful extension. Display the number of folders and files searched and the number of matches (and search time ;)). A text below the treeview would be sufficient.

 

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 post
Share on other sites

had some time to kill so i have updated _GUICtrlTuner_Limitless.au3 to work with this gui 

Anyways.  here is a PS version to what i have already pointed out here 

The way its done here is that the search isn't restricted to any parameter for instance delete the filter and it will just list any latest files to the top

the only parameter that is restricted is the "years" one set to -1 year back (by default)

Spoiler
#include <GUIConstants.au3>
#include <GuiTreeView.au3>
#include <File.au3>
#include <GuiComboBox.au3>

Const $SCALING = CalcDPI()
Local $fTypes = '*.a3x, *.ani, *.asf, *.au3, *.avi, *.bak, *.bat, *.bin, *.bmp, *.cab, *.chm, *.cvs, *.dat, *.dll, *.doc, *.exe, *.html, *.ini, *.log, *.m4p, *.m4r, *.m4v, *.mkv, *.mov, *.mp3, *.mp4, *.mpa, *.mpg, *.msi, *.pdf, *.rar, *.raw, *.rdc, *.reg, *.rtf, *.tif, *.tmp, *.ttf, *.txt, *.pdf, *.xls'
$fTypes = StringReplace($fTypes, ", ", ",")
Local $FileListType = StringSplit($fTypes, ", ")

$hGUI = GUICreate("Search Content", 816, 594, 190, 120, $WS_THICKFRAME + $GUI_SS_DEFAULT_GUI)
GUISetFont(11)
Local $idSelect = GUICtrlCreateButton("Select Folder", 48, 20, 110, 29)
Local $idFolder = GUICtrlCreateCombo(@Compiled ? $cmdline[1] : @ScriptDir, 168, 23, 475, 24, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlCreateLabel("Filter :", 653, 26, 44, 20)
$idFilter = GUICtrlCreateCombo("", 699, 23, 71, 23)
$cData = ""
For $i = 1 To UBound($FileListType) - 1
    $cData &= "|" & $FileListType[$i]
Next
GUICtrlSetData($idFilter, $cData)
_GUICtrlComboBox_SetCurSel($idFilter, 3)
Local $StaticHit = GUICtrlCreateLabel("Hits:", 187, 146, 26, 20)
Global $iHitCount = GUICtrlCreateInput(0, 217, 142, 64, 24)
GUICtrlCreateUpdown($iHitCount)
GUICtrlCreateLabel("Level :", 48, 146, 44, 20)
Global $IdLevel = GUICtrlCreateInput(0, 97, 142, 71, 24)
GUICtrlCreateUpdown($IdLevel)
GUICtrlCreateLabel("Text to search :", 344, 103, 102, 20)
$idText = GUICtrlCreateInput("", 451, 101, 318, 22)
$idTreeFile = GUICtrlCreateTreeView(19, 181, 777, 345)
GUICtrlSetFont(-1, 9)
$idSearch = GUICtrlCreateButton("Search", 108, 547, 100, 25)
$idOpen = GUICtrlCreateButton("Open", 358, 547, 100, 25)
$idReset = GUICtrlCreateButton("Reset", 608, 547, 100, 25)
$Static19 = GUICtrlCreateLabel("TimeSpan :", 48, 103, 75, 20)
$IdLabel26 = GUICtrlCreateLabel("FileLike", 49, 67, 49, 18)
$IdInput29 = GUICtrlCreateInput("*", 106, 63, 286, 22)
GUICtrlSetFont(-1, 12)
$IdLabel27 = GUICtrlCreateLabel("FileXpr", 409, 63, 49, 22)
$IdInput28 = GUICtrlCreateInput("^", 463, 63, 307, 22)
GUICtrlSetFont(-1, 12)
$Edit20 = GUICtrlCreateCombo("", 131, 100, 71, 23)
GUICtrlSetData($Edit20, "Hours|Days|Months|Years")
_GUICtrlComboBox_SetCurSel($Edit20, 3)
$Static21 = GUICtrlCreateLabel("Count :", 209, 103, 48, 20)
$Edit22 = GUICtrlCreateInput(0, 264, 99, 64, 24)
GUICtrlCreateUpdown($Edit22)
GUISetState()

Local $sFolder, $idTVselect, $aSelect[1]

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
            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 $idReset
            ReDim $aSelect[1]
            $aSelect[0] = 0
            GUICtrlSetData($idText, "")
            GUICtrlSetData($iHitCount, 0)
            GUICtrlSetData($IdLevel, 0)
            _GUICtrlTreeView_DeleteAll($idTreeFile)
    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 $iFound = 0, $bHit = GUICtrlRead($iHitCount), $iHit = 0
    GUISetCursor($CURSOR_WAIT, $GUI_CURSOR_OVERRIDE)
    _GUICtrlTreeView_DeleteAll($idTree)
    $aFileTime = _getlast($sFolder, $sFilter, $sText)
EndFunc
Func _getlast($sSrc, $sFilter, $sText = "")
    Local $File, $i = GUICtrlRead($IdLevel)
    If Not StringRegExp($sSrc, '^.:\\$') Then $sSrc &= '\'
    $sFilter = StringReplace($sFilter, "*", "")
    $DirPath = $sSrc
    $sPath = " -Path " & $DirPath & " "
    $iDepth = $i <= 0 ? " -Recurse" : " -Depth " & $i
    Local $Extension = $sFilter, $fileExtension = ""
    If $sFilter Then
        $Extension = $sFilter
        $fileExtension = "| Where-Object{$_.extension -eq '" & $Extension & "'}"
    EndIf
    Local $sTimeSpan[4] = ["Hours", "Days", "Months", "Years"]
    $sTimeSpan = GUICtrlRead($Edit20)
    $iCountSelect = GUICtrlRead($Edit22)
    
    ; if $iCountSelect = 0 then $iCountSelect =  -1  Year (default)
    $iCountSelect = $iCountSelect <= 0 ? "-1" : GUICtrlRead($Edit22) * -1
    $iCountSelectTimeSpan = "| where {$_.lastwritetime -gt (get-date).add" & $sTimeSpan & "('" & $iCountSelect & "')}"
    
    $DislpayiHits = ""
    $iHits = GUICtrlRead($iHitCount)
    If $iHits > 0 Then $DislpayiHits = "|Select-Object -First " & $iHits
    Local $sfilenameLike = GUICtrlRead($IdInput29), $sfilename1 = ""
    If $sfilenameLike Then $sfilename1 = "| where {$_.Name -like '" & $sfilenameLike & "'}"
    Local $sfilenameMatch = GUICtrlRead($IdInput28), $sfilename2 = ""
    $sfilename2 = "| where {$_.Name -match  '" & $sfilenameMatch & "'}"
    $sSearchString = $sText
    $sSearch = "| Where-Object { $_ | Select-String -Pattern '" & $sSearchString & "' }"
    If Not $fileExtension Or Not $sSearchString Then $sSearch = ""
    $bLatest = "| Sort-Object -Descending lastwritetime"
    $PSI = "| Where-Object { !$_.PSIsContainer} "
    GUICtrlCreateTreeViewItem("Path = " & @TAB & ($sPath ? $DirPath : @ScriptDir), $idTreeFile)
    GUICtrlCreateTreeViewItem("filename1 = " & @TAB & ($sfilename1 ? $sfilenameLike : False), $idTreeFile)
    GUICtrlCreateTreeViewItem("filename2 = " & @TAB & ($sfilename2 ? $sfilenameMatch : False), $idTreeFile)
    GUICtrlCreateTreeViewItem("Extension = " & @TAB & ($fileExtension ? $Extension : False), $idTreeFile)
    GUICtrlCreateTreeViewItem("Search = " & @TAB & ($sSearch ? $sSearchString : False), $idTreeFile)
    GUICtrlCreateTreeViewItem("CountBack = " & @TAB & ($iCountSelectTimeSpan ? "Last " & $sTimeSpan & ":" & $iCountSelect : False), $idTreeFile)
    GUICtrlCreateTreeViewItem("Level Depth = " & @TAB & ($iDepth ? $i : False), $idTreeFile)
    GUICtrlCreateTreeViewItem("Hits = " & @TAB & ($DislpayiHits ? $iHits : False), $idTreeFile)
    $sCommands = $sPath & $iDepth & $fileExtension & $PSI & $iCountSelectTimeSpan & $sfilename1 & $sfilename2 & $sSearch & $DislpayiHits & $bLatest & " | foreach {$_.FullName}"
    $sCommands = 'powershell -Command "Get-ChildItem' & $sCommands
    $iPID = Run(@ComSpec & " /c " & $sCommands, "", @SW_HIDE, $stdout_child)
    $sOutput = ""
    While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then
            ExitLoop
        EndIf
    WEnd
    GUISetCursor()
    Local $Array[0][2]
    _ArrayAdd($Array, $sOutput, "|")
    _GUICtrlTreeView_DeleteAll($idTreeFile)
    For $i = 0 To UBound($Array) - 1
        GUICtrlCreateTreeViewItem($Array[$i][0], $idTreeFile)
    Next
EndFunc
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)
        EndIf
    Next
    ControlTreeView($hGUI, "", $idTree, "Expand", $sItem)
EndFunc
Func OpenFile($sFile, $sText)
    Local $sDrive, $sDir, $sFileName, $sExtension
    _PathSplit($sFile, $sDrive, $sDir, $sFileName, $sExtension)
    ClipPut($sText)
    ShellExecute($sFile, "", $sDrive & $sDir, "open")
EndFunc
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

 

 

Edited by Deye
Link to post
Share on other sites

@Deye  Thank you for that suggestion.  I see now what was the intent of your previous post.  But the goal of the script is not to find specific files. Its goal is to search for specific content inside files.  For example, I was also thinking of using RegExp.  However, the RegExp would be applied to text in files not to file names.  It is also important for me that the filter field can support multiple extensions.  When I search for FreeBasic content, I need to search for both .bas and .bi.

Of course I could merge all your features and mine into a single tool, but I need to think about.  Thanks again for taking time to adapt your idea to the script.  Maybe others can share their thoughts ?

Link to post
Share on other sites

 

34 minutes ago, Nine said:

goal is to search for specific content inside files

look again with the "Text to search" field < that allows to do exactly that (search within files)

i'm sure there are more commands that will allow you to search with RegExp

if you had to get all these features using just autoit's keys you would have to put in much more work to update the OP example

This example off course doesnt override the OP example juat another example on how to do it all plus with more features @incorporating  PS

Edited by Deye
Link to post
Share on other sites
  • 3 months later...

Nice tool.

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * for other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2021-10-18

Link to post
Share on other sites
3 minutes ago, MattHiggs said:

but does your tool support Regular Expressions

No, sorry.  I played a bit with it, but felt it was kind of slow and I didn't feel the need to it...

Link to post
Share on other sites
On 4/8/2020 at 9:11 AM, Nine said:

* Allows only 1 running instance of the script

It doesn't bother me, but is there any reason running two instances at the same time would be detrimental to ... anything? I generally don't do this in my scripts because I couldn't care less if you have two instances open

(Side note: I like that the date -in the quote- is wrong due to the edits :D)

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

Link to post
Share on other sites
11 minutes ago, seadoggie01 said:

I couldn't care less if you have two instances open

Sometimes I forget I have this running so I was always creating more and more instances.  After few days I had like 8 instances of it.  I do not see any usage of having more than 1 instance of this tool open.

14 minutes ago, seadoggie01 said:

date -in the quote- is wrong

:)

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...