Jump to content
Sign in to follow this  
cramaboule

Search and search IN file

Recommended Posts

cramaboule

Here my srcipt for searching files and/or searching inside files.

Made long time ago with the help of this forum.

#include <File.au3>
#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Global $sRet[1] = ["0"], $flag, $percent
Local $j, $aArayFound[1], $a = 0, $b = 0, $sFolder = "", $debug = 0

;~ _ArrayDisplay( $sRet , "$sRet")

#Region ### START Koda GUI section ### Form=
$Gui = GUICreate("Search Engine", 554, 559, -1, -1)
If $debug Then
    $iFolder = GUICtrlCreateInput("C:\Users\Admin\au3", 160, 10, 121, 21, $ES_READONLY)
    $iFilter = GUICtrlCreateInput("*.au3", 160, 40, 121, 21)
    $iWord = GUICtrlCreateInput("gui", 160, 70, 121, 21)
Else
    $iFolder = GUICtrlCreateInput("Folder to search in", 160, 10, 121, 21, $ES_READONLY)
    $iFilter = GUICtrlCreateInput("filter *.*", 160, 40, 121, 21)
    $iWord = GUICtrlCreateInput("word inside file", 160, 70, 121, 21)
EndIf
$bSearch = GUICtrlCreateButton("Search", 290, 40, 91, 50, $BS_DEFPUSHBUTTON)
$bChoose = GUICtrlCreateButton("Choose Folder", 290, 10, 91, 21)
$lLabel = GUICtrlCreateLabel("", 6, 130, 541, 70)
$pProgress = GUICtrlCreateProgress(160, 100, 217, 25)
$eEdit = GUICtrlCreateEdit("", 3, 201, 548, 354)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUICtrlSetLimit($eEdit, 2147000000)

;~ GUICtrlSetData( $iFolder, "C:\")
;~ GUICtrlSetData( $iFilter, "*.aif")
;~ GUICtrlSetData( $iWord, "")


;http://www.autoitscript.com/forum/topic/126552-solved-guictrlcreateinput-question/
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $bChoose
            $sPath = FileSelectFolder("Choose Folder", "", 0, "", GUICreate(""))
;~             $sPath=FileOpenDialog ( "Choose", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "All (*.*)" , 7 , "", GUICreate("") )
            GUICtrlSetData($iFolder, $sPath)
        Case $GUI_EVENT_PRIMARYDOWN
            $aCInfo = GUIGetCursorInfo($Gui)
            If $aCInfo[4] = $iFilter And $a = 0 Then
                GUICtrlSetData($iFilter, "")
                $a = 1
            EndIf
            If $aCInfo[4] = $iWord And $b = 0 Then
                GUICtrlSetData($iWord, "")
                $b = 1
            EndIf
        Case $bSearch
            $flag = 0
            $f = 1
            If Not (StringInStr(GUICtrlRead($iFolder), ":\")) Then
                $MsgBox = MsgBox(5 + 16, "Error", "The input folder is not correct:" & @CRLF & "Retry = Choose an another Folder" & @CRLF & "Cancel = Exit")
                If $MsgBox <> 4 Then
                    Exit
                Else
                    $f = 0
                EndIf
            EndIf
            If StringInStr(GUICtrlRead($iFilter), "Filter") And $f = 1 Then
                $MsgBox = MsgBox(5 + 16, "Error", "The input filter is not correct:" & @CRLF & "Retry = Choose an another Filter" & @CRLF & "Cancel = Exit")
                If $MsgBox <> 4 Then
                    Exit
                Else
                    $f = 0
                EndIf
            EndIf
            If StringInStr(GUICtrlRead($iWord), "word inside file") Then
                GUICtrlSetData($iWord, "")
            EndIf
            If StringRight(GUICtrlRead($iFolder), 1) = "\" Then
                $sFolder = StringTrimRight(GUICtrlRead($iFolder), 1)
            Else
                $sFolder = GUICtrlRead($iFolder)
            EndIf
            If $f Then
                GUICtrlSetData($eEdit, "Please wait...")
                _Search(GUICtrlRead($iWord), GUICtrlRead($iFilter), $sFolder)
            EndIf
    EndSwitch
WEnd





Func _Search($sSearchString, $sFindFile, $sPath)
;~     ConsoleWrite($sSearchString&"--"&$sFindFile&"--"&$sPath & @LF)
    Dim $aRetArray[1], $aArayFound[1]
    Global $sRet[1] = ["0"]
    $j = 0
    $aRetArray = _FindPathName($sPath, $sFindFile, $sSearchString)
    If $aRetArray[0] = "0" Then
;~         ReDim $aRetArray[2]
;~         $aRetArray[0] = 1
;~         $aRetArray[1] = "File not found"
        $sEdit = "File not found"
    EndIf
    _UpdateBar(2)
    If $sSearchString <> "" Then
        For $i = 1 To $aRetArray[0]
            $file = FileOpen($aRetArray[$i], 0)
            _UpdateBar(0, $i, $aRetArray[0])
            If $file = -1 Then
            Else
                $text = FileRead($file)
                If StringInStr($text, $sSearchString) > 0 Then
                    $j += 1
                    ReDim $aArayFound[$j + 1]
                    $aArayFound[$j] = $aRetArray[$i]
                    If $j=1 Then GUICtrlSetData($eEdit, "")
                    GUICtrlSetData($eEdit, $aArayFound[$j] & @CRLF,1)
                EndIf
            EndIf
            FileClose($file)
        Next
;~             _ArrayDisplay($aArayFound, "List of file with the String: "&$sSearchString&" found")
        If (Not $j) Then
            $sEdit = "File not found"
            GUICtrlSetData($eEdit, $sEdit)
;~         Else
;~             $sEdit = ""
;~             For $i = 1 To UBound($aArayFound) - 1
;~                 $sEdit &= $aArayFound[$i] & @CRLF
;~             Next
        EndIf
;~         GUICtrlSetData($eEdit, $sEdit)
        _UpdateBar(2)
    Else
;~             _ArrayDisplay($aRetArray,"List of folders found")
;~         $sEdit = ""
;~         For $i = 1 To UBound($aRetArray)-1
;~             $sEdit &= $aRetArray[$i] & @CRLF
;~         Next
;~             GUICtrlSetData($eEdit, $sEdit)
    EndIf
EndFunc ;==>_Search

;~ http://www.autoitscript.com/forum/topic/95160-file-searcher
;~ In this forum All files where not Found...
;~ So here is the new way of doing it (Made By Cramaboule)
; Searches all subfolders of $sPath for $sFindFile (* and ? wildcards accepted)
; Returns an array containing full path and name of all matches.
; Number of matches is in zero index of array
Func _FindPathName($sPath, $sFindFile, $sSearchInFile)
    Local $sSubFolderPath, $iIndex, $aFolders
    GUICtrlSetData($lLabel, $sPath)
    _UpdateBar(1)
    $aFolders = _FileListToArray($sPath, "*", 2)
    $error = @error
    $aFile = _FileListToArray($sPath, $sFindFile, 1)
    If Not (@error) Then ; no files
        For $k = 1 To $aFile[0]
            _ArrayAdd($sRet, $sPath & "\" & $aFile[$k])
            If $sSearchInFile = "" Then
                $flag += 1
                If $flag = 1 Then
                    GUICtrlSetData($eEdit, $sPath & "\" & $aFile[$k] & @CRLF); efface uniquement la première fois !
;~                     ConsoleWrite($sPath& "\"&$aFile[$k]& @CRLF)
                EndIf
                GUICtrlSetData($eEdit, $sPath & "\" & $aFile[$k] & @CRLF, 1)
;~                 ConsoleWrite($sPath& "\"&$aFile[$k]& @CRLF)
            EndIf
        Next
        $sRet[0] = UBound($sRet) - 1
    EndIf
    If Not ($error) Then ; no folders
        For $i = 1 To $aFolders[0]
            $sSubFolderPath = $sPath & "\" & $aFolders[$i]
            _FindPathName($sSubFolderPath, $sFindFile, $sSearchInFile)
        Next
    EndIf
    Return $sRet
EndFunc ;==>_FindPathName

Func _UpdateBar($Update = 0, $actuel = 0, $total = 0)
    If $Update = 1 Then
        $percent += 1
        If $percent >= 100 Then
            $percent = 0
        EndIf
    ElseIf $Update = 0 Then
        $percent = Ceiling(($actuel / $total) * 100)
        GUICtrlSetData($pProgress, $percent)
    ElseIf $Update = 2 Then
        $percent = 100
    EndIf
;~     ConsoleWrite($percent & " " & $actuel & " " & $total & @CRLF)
    GUICtrlSetData($pProgress, $percent)
EndFunc ;==>_UpdateBar

Enjoy.

C.

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
Sign in to follow this  

  • Similar Content

    • smud
      By smud
      Currently, I'm working on a program that will display Dialog boxes with either Yes or No.
      For each dialog, I reward the user with X amount of Credits.
      I'm hoping to output the amount of credits to a cell in a column (there will be 20 different columns).
      It will only post to a row that is equal to today's date (first column). If no row exists yet with the current date, it will start a new row.
      Any suggestions?
      Thank you
    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • nacerbaaziz
      By nacerbaaziz
      hi dears
      am using an ini files as a History
      Sometimes the file size is be larger to 5 MB
      AutoIt does not recognize the full content of the file
      When I did a search to find out why, I discovered that INI files could not be read if they size larger than 64 KB.
      for that I preferred to ask here if is there any way to bypass this obstacle.
      The contents of the file are  as follows
      [filesList]
      c:\...\...\f1.mp3=00:15:20
      c:\...\...\f2.mp3=00:10:20
      c:\...\...\f3.wav=00:59:20
      ....... Etc
      This is the section for reading the file (adapted from my main script)

      case $continue     Local $aArray = IniReadSection($WaitingListFile, StringEncrypt(true, "filesList", $MyPassword))     If Not @error Then Opt("GUICloseOnESC", 1) _GUICtrlListView_DeleteAllItems($scList)         For $i = 1 To $aArray[0][0] $path = path_list(StringEncrypt(false, $aArray[$i][0], $MyPassWord), 1) if FileExists(StringEncrypt(false, $aArray[$i][0], $MyPassWord)) then GUICtrlCreateListViewItem(_GetFileName(FileGetLongName(StringEncrypt(false, $aArray[$i][0], $MyPassWord))) & Opt("GUIDataSeparatorChar") & " : " & Opt("GUIDataSeparatorChar") & FileGetLongName(StringEncrypt(false, $aArray[$i][0], $MyPassWord)), $scList) else $path = $path endIf         Next GUISetState(@sw_disable, $hGUI) GUISetState(@sw_show, $hGUI2) GUICtrlSetState($SClist, $GUI_FOCUS) else if $accessibilitymode = 1 then speak(str("listEmpty")) endIf endIf
      Is there any way to solve this problem, please?
      am waiting your answers...
      Greetings to All
    • caramen
      By caramen
      Hey.  
       
      I requested help about how to get a value from a text in a variable. 
      Now i know how to do that. But i learned with the command FileRead. Now i whould like to know how to replace the command :
      FileRead('Dossier.txt') The purpose is to read a webpage text. To find some value inside. 
       
      Btw i tryed to play with WindowsInfo.au3 but i dont got much thing.  
    • AndyS19
      By AndyS19
      I'm trying to implement a Ctl-F popup box that looks something like the one that Notepad uses, but I'm not havine much luck.  I intend to get it working, then beef up the popup's contents to add several checkboxes, buttons and radio boxes.
      What my example code does is to use InputBox(), but that's not what I want.
      Here is my test code:
      #include <Array.au3> #include <GUIConstantsEx.au3> #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 Opt("GUICloseOnESC", 1) Opt("GUIOnEventMode", 1) Opt('MustDeclareVars', 1) OnAutoItExitRegister("ExitStageLeft") Opt("WinTitleMatchMode", -2) Global $hGUI _Main() Func _Main() $hGUI = GUICreate("Test ^F", 300, 200) setupSpecialKeysHandlers() GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose") GUISetState() While (1) Sleep(157) WEnd EndFunc ;==>_Main Func handle_CTRL_F_key() Local $str $str = InputBox("Search", "Enter the string to search for:") ConsoleWrite("+++: $str ==>" & $str & "<==" & @CRLF) EndFunc ;==>handle_CTRL_F_key Func ExitStageLeft() Exit (99) EndFunc ;==>ExitStageLeft Func Event_GUIClose() Exit (1) EndFunc ;==>Event_GUIClose Func setupSpecialKeysHandlers() Local $ar, $parts, $key, $handler, $id Local $aAccelKeys[1][2] ; Create a table of Special keys and their handlers $ar = StringSplit("", "") _ArrayAdd($ar, "^f - handle_CTRL_F_key ") ReDim $aAccelKeys[UBound($ar) - 1][2] ; Now, create $aAccelKeys array with the table data. ; For each entry, create a Dummy GUI and associate its ; ID with the special key. For $ndx = 1 To UBound($ar) - 1 $parts = StringSplit($ar[$ndx], "-", 2) $key = StringStripWS($parts[0], 8) $handler = StringStripWS($parts[1], 8) $id = GUICtrlCreateDummy() $aAccelKeys[$ndx - 1][0] = $key $aAccelKeys[$ndx - 1][1] = $id GUICtrlSetOnEvent($id, $handler) Next GUISetAccelerators($aAccelKeys) ; Setup the Special keys hooks EndFunc ;==>setupSpecialKeysHandlers  
×