Jump to content
Nine

Searching specific content in Text file or AU3

Recommended Posts

Posted (edited)
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

Share this post


Link to post
Share on other sites
9 minutes ago, avikdhupar said:

Error in new Version:  "Invalid counter at line 1204"

Unable to reproduce that error.  Do you have more information how you got that ?

Share this post


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 ?

Share this post


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."

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


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 ?

Share this post


Link to post
Share on other sites
Posted (edited)

 

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

Share this post


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...