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

    • VADemon
      By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
    • 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. 
    • Blueman
      By Blueman
      Hey Guys,
      Hope that you can help me with something, maybe this is a bug in the new version of AUTOIT but first i will check it with you to know for sure.
      I have made a simple GUI with a Embedded IE Object, then i would like to read the HTML with _IEBodyReadHTML(), easy right?
      When i use the old IE.au3 include from a year back or so, it is working fine!
      When i use the new IE.au3 include came with the new installation that is currently available on autoitscript.com it isnt working (i get a result that says; 0).
      Let me show you.
       
      Working Example
      #include <GUIConstantsEx.au3> #include <IE_EmbeddedVersioning.au3> #include <IE_PreVersion.au3> ;Older Version Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 1000, 1000) Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) Global $oIE_1 = _IECreateEmbedded() ; CREATE IE OBJECT(S) GUICtrlCreateObj($oIE_1, 355, 5, 600, 360) _IENavigate($oIE_1, "https://www.google.nl", 1) Local $CheckHTML_T = _IEBodyReadHTML($oIE_1) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) MsgBox(48,"",$CheckHTML_T) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idOK ExitLoop EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example  
      Failing Example
      #include <GUIConstantsEx.au3> #include <IE_EmbeddedVersioning.au3> #include <IE.au3> ;New Version Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 1000, 1000) Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) Global $oIE_1 = _IECreateEmbedded() ; CREATE IE OBJECT(S) GUICtrlCreateObj($oIE_1, 355, 5, 600, 360) _IENavigate($oIE_1, "https://www.google.nl", 1) Local $CheckHTML_T = _IEBodyReadHTML($oIE_1) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) MsgBox(48,"",$CheckHTML_T) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idOK ExitLoop EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example  
      I have attachted all files and i am testing on Windows 10 with the latest SciTe Program (Not compiled). 
      When i compile the script it is showing the same result.
      Thanks guys!
      IE_PreVersion.au3
      IE.au3
      IE_EmbeddedVersioning.au3
    • 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
    • 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
×