Sign in to follow this  
Followers 0
cramaboule

Search and search IN file

1 post in this topic

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  
Followers 0

  • Similar Content

    • Gowrisankar
      By Gowrisankar
      Hello everyone,
      I'm working on a task where, a PDF file is opened (in IE browser) when I click a link in a website.
      I have to read the first page of the PDF to find particular strings. Can you please share some ideas?
    • rudi
      By rudi
      Hello.
      I'm too stupid to see my mistake:
      To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.
      Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting
      The "TIFF Header Format" is easy:
      Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)
      --> II
      Offset 02h, 2 Byte = Version Nr.
      Offset 04h, 4 Byte = pointer to first IFD entry
      Description of TIFF header: https://www.awaresystems.be/imaging/tiff/faq.html#q3
       

      Howto read and analyse the binary content correctly? This is my messy, not operational code:
       
      $sampleTiff="H:\daten\tif\11\11\111111.TIF" $h=FileOpen($sampleTiff,16) $content=FileRead($h) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console FileClose($h) $type=VarGetType($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $ToString=BinaryToString($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToString = ' & $ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & @CRLF) $content=StringTrimLeft($content,2) ; cut off the leading "0x" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console for $i = 1 to 8 step 8 $next=StringMid($content,$i,2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $next = ' & $next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $Chr=BinaryToString($next) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Chr = ' & $Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & "---" & @CRLF) Next Regards, Rudi.
      111111.TIF
    • DeathChicken
      By DeathChicken
      If possible please add or edit the comments to explain how this works.
       
      ;includes functions from other things
      #include <GDIPlus.au3>
      #include <ScreenCapture.au3>
      ;hotkeys
      HotKeySet("{ESC}", _exit)
      HotKeySet("{F1}", _scan)
      ;global variables
      Global $win_title                    ;name of the window
      Global $area_x                        ;
      Global $area_y                        ;
      Global $area_w                       ;
      Global $area_h                        ;
      Global $cursor                         ;
      Global $rect_file                      ;
      Global $hbmpscreen              ;
      Global $i=  1043                      ;moves the rect
      Global $ii=    378                      ;moves the rect
      Global $x=1044                        ;inner rect offset
      Global $y=501                          ;inner rect offset
      Global $hbmprect                    ;image inside rect
      Global $hscreen                        ;image whole screen
                                         
      _GDIPlus_Startup()                    ;?
      ;display hotkeys on screen
      ToolTip("Press F1 to scan | Press ESC to Exit",0,0)
                                          ;infinite loop to keep prog running
      While 1
          Sleep(100)
      WEnd
      Func _scan()
      ;~ reads your screen area:
      _read()
      ;~ converts screen captured into bmp
      _convert()
      ;~ .  Loads converted bmp to be read
      _loadBMP()
      ;~ compares the bmp of your scanned screen to the actual screen that is being displayed
      _compare()
      EndFunc
                                          ;reads screen
      Func _read()
          $hscreen = _ScreenCapture_CaptureWnd("", WinGetHandle($win_title), $area_x, $area_y, $area_x + $area_w, $area_y + $area_h, $cursor)
      EndFunc
                                          ;converts screen into bmp
      Func _convert()
          _GDIPlus_BitmapCreateFromHBITMAP($hscreen)
      EndFunc
                                          ;loads converted screen
      Func _loadBMP()
      _GDIPlus_BitmapCreateFromFile($rect_file)
      EndFunc
                                          ;compares savedBMP to current screen
      Func _compare()
      ;If(                              below code                                     ) = (                   below code           ) Then
      If _GDIPlus_BitmapGetPixel($hbmpscreen, ($i + $x) - $area_x, ($ii + $y) - $area_y) = _GDIPlus_BitmapGetPixel($hbmprect, $x, $y) Then
      ;display message box titled found with a message of found
      MsgBox("","","found")
      ;if above is not correct then
      EndIf
      EndFunc
                                          ;exit func
      Func _exit()
          Exit
      EndFunc
    • algiuxas
      By algiuxas
      Hello everybody, so I might found a bug in _ArraySearch:
       
      #include <Array.au3> Local $abc[2] = ["b", "b"] $h = "+---------------------------------+" & @CRLF ConsoleWrite($h) For $i = 0 To 1 $abc[0] = 0 out() $abc[0] = 1 out() $abc[0] = -1 out() $abc[0] = "Abc" out() $abc[1] = "Hello" Next Func txt() Return "$abc = [" & $abc[0] & ", " & $abc[1] & "]" & @CRLF & _ "> _ArraySearch($abc,""Hello"") = " EndFunc ;==>txt Func shouldoutput($ans) Return "_ArraySearch Should return: " & ($ans ? 1 : -1) EndFunc ;==>shouldoutput Func out() ConsoleWrite("> " & txt() & _ArraySearch($abc, "Hello") & @CRLF & "> " & shouldoutput($abc[1] == "Hello") & @CRLF & $h) EndFunc ;==>out Exit Sorry for this messy script