Jump to content

Search and search IN file


cramaboule
 Share

Recommended Posts

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.

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

×
×
  • Create New...