Jump to content
Deye

_FileListToArrayRec

Recommended Posts

Posted (edited)

I  know that _FileListToArrayRec has to deal with many switches aside from the basic looping but some how I'm sure it will be possible to shave some of the execution time,  with perhaps following the logic i have used in my example

another thing i found is that when searching for both file and folders with using a negative per-fixed number (depth switch), I think it wont return a correct array :blink: maybe I missed reading something in the help file, will soon check again ..

anyways here is the comparison I came up with ..

Spoiler
#include <File.au3>
#include <String.au3>
#include <GUIConstantsEx.au3>

$aArray = DriveGetDrive("ALL")
For $i = 1 To $aArray[0]
    $aArray[$i] = StringUpper($aArray[$i])
Next
$sString = _ArrayToString($aArray, "|", 1)

$hGUI = GUICreate("FileListLevel", 320, 125)
$hCombo = GUICtrlCreateCombo("", 10, 8, 44, 20)
GUICtrlSetFont(-1, 12)
GUICtrlSetData(-1, $sString)

$idFileInput = GUICtrlCreateInput("*", 65, 8, 125, 25)
GUICtrlSetFont(-1, 12)

$hCombo2 = GUICtrlCreateCombo("SearchAllInDrive", 10, 70, 180, 20)
GUICtrlSetFont(-1, 13)
GUICtrlSetData(-1, "1|2|3|4|5|6|7|8|9|10")

$Button = GUICtrlCreateButton("Search", 215, 08, 100, 25)
GUICtrlSetFont(-1, 12)
$ButtonCancel = GUICtrlCreateButton("Abort Search", 215, 45, 100, 25)
GUICtrlSetFont(-1, 12)
$ButtonResult = GUICtrlCreateButton("Copy result", 215, 80, 100, 25)
GUICtrlSetFont(-1, 12)

$File = GUICtrlCreateRadio("File", 10, 38, 50, 25)
GUICtrlSetFont(-1, 12)
GUICtrlSetState(-1, $GUI_CHECKED)
$Folder = GUICtrlCreateRadio("Folder", 65, 38, 70, 25)
GUICtrlSetFont(-1, 12)

$bBoth = GUICtrlCreateCheckbox("Both", 144, 40, 70)
GUICtrlSetFont(-1, 12)

Global $iCount, $iTotalCount = 500, $aArray, $aResult, $sPattern, $sFilePath = @ScriptDir & "\TestUniqueResults1.txt", $bCancel = False

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            $iLevel = GUICtrlRead($hCombo2)
            If BitAND(GUICtrlRead($bBoth), $GUI_CHECKED) Then
                $bFile = 2
            Else
                $bFile = BitAND(GUICtrlRead($File), $GUI_CHECKED) ? 1 : 0
            EndIf
            _Run(GUICtrlRead($hCombo) & GUICtrlRead($idFileInput), StringIsAlpha($iLevel) = 1 ? 0 : $iLevel, $bFile)
        Case $ButtonCancel
            $bCancel = True
        Case $ButtonResult
            _FileWriteFromArray($sFilePath, $aResult, 1)
    EndSwitch
WEnd

Func _gui()
    Switch GUIGetMsg()
        Case $ButtonCancel
            $bCancel = True
            AdlibUnRegister("_gui")
            ConsoleWrite(@LF & "Canceling .. " & @LF & @LF)
    EndSwitch
EndFunc   ;==>_gui

Func _Run($sSearchTerm, $iEndLevel, $bFile = 0)
    $bCancel = False
    $aResult = _Search($sSearchTerm, $iEndLevel, $bFile)
    ConsoleWrite("Done Searching" & @LF & @LF)
EndFunc   ;==>_Run

Func _Search($sSearch, $iEndLevel = 0, $bFile = 1)
    Local $a = StringSplit($sSearch, ":"), $sPath
    $sPattern = "(?i)^" & StringReplace(StringRegExpReplace($a[2], "[][$^.{}()+\-]", "\\$0"), "*", ".*?") & "$"

    AdlibRegister("_gui", 100)  ;comment this line if using _ArrayDisplays otherwise the script hangs or use Adlib Un\Register("_gui") before\after displaying
    Local $iFileType, $iIndex, $hTimer, $iStart = 1, $iRecur
    If $iEndLevel = 0 Then $iStart = 0

    For $i = $iStart To $iEndLevel
        ConsoleWrite("Level = " & $i & @LF)
        If $bCancel Then Return

        $hTimer = TimerInit()
        $iTotalCount = 500
        $iCount = 1
        Dim $aArray[$iTotalCount]
        If $bCancel Then Return
        _GetArray($i, $bFile, $a[1] & ":", "", $a[2])
        If $bCancel Then Return
        $iIndex = _ArraySearch($aArray, "", 1)
        $iIndex = (@error = -1 ? UBound($aArray) : $iIndex - 1)
        ConsoleWrite("Time Other  : " & TimerDiff($hTimer) & " UBound = " & $iIndex & @LF)

        ;Section for displaying the array
;~      Local $aRet = _ArrayExtract($aArray, 0, $iIndex)
;~      $aRet[0] = UBound($aRet) - 1
;~      AdlibUnRegister("_gui")
;~      _ArrayDisplay($aRet)
;~      AdlibRegister("_gui", 100)

        ; _FLTAR ()
        Switch $bFile ;convert file folder indications when using _FLTAR
            Case 0
                $iFileType = 2 ;folder
            Case 1
                $iFileType = 1 ;file
            Case 2
                $iFileType = 0 ;both
        EndSwitch

        $iRecur = ($i - 1) * -1
        If $iEndLevel = 0 Then $iRecur = 1

        $hTimer = TimerInit()
        Local $aRet = _FileListToArrayRec($a[1] & ":\", $a[2], $iFileType, $iRecur, 0, 2)
        ConsoleWrite("Time ArrayRec: " & TimerDiff($hTimer) & " UBound = " & UBound($aRet) - 1 & @LF)
        ConsoleWrite("===========================" & @LF & @LF)

;~      AdlibUnRegister("_gui")
;~      _ArrayDisplay($aRet)
;~      AdlibRegister("_gui", 100)

    Next
    AdlibUnRegister("_gui")
EndFunc   ;==>_Search

Func _GetArray($iEndLevel, $bFile, $sPath, $File = "", $iLevel = 0)
    If StringRegExp($File, $sPattern) Then
        If $iCount >= $iTotalCount - 1 Then
            $iTotalCount += $iTotalCount
            ReDim $aArray[$iTotalCount]
        EndIf
        Local $hSearch = FileFindFirstFile($sPath & $File & '\*')
        If $hSearch = -1 Then
            Switch $bFile
                Case 0                    ;Dir with Exp match
                    $aArray[$iCount] = $sPath & $File & "\"
                    $iCount += 1
                Case $bFile = @error    ;$bFile = 1 but @error = 1 an Empty Dir with Exp match
                    ;Nothing
                Case Else               ; $bFile = 0 or 1
                    $aArray[$iCount] = $sPath & $File
                    $iCount += 1
            EndSwitch
            Return
        ElseIf (1 - $bFile) Then        ; Folders Switch $bFile = 0 \ 2
            $aArray[$iCount] = $sPath & $File & "\"
            $iCount += 1
        EndIf
        If $iEndLevel > 0 And $iLevel = $iEndLevel Then Return
    Else
        If $iEndLevel > 0 And $iLevel = $iEndLevel Then Return
        $hSearch = FileFindFirstFile($sPath & $File & '\*')
        If $hSearch = -1 Then Return
    EndIf
    Local $Result = $sPath & $File
    While 1
        $File = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If (Not @extended) Then
            If Not $bFile Or Not StringRegExp($File, $sPattern) Then ContinueLoop
        EndIf
        _GetArray($iEndLevel, $bFile, $Result & "\", $File, $iLevel + 1)
        If $bCancel Then Return
    WEnd
    FileClose($hSearch)
EndFunc   ;==>_GetArray

 

Deye

Edited by Deye
Added readability pointers to the loop

Share this post


Link to post
Share on other sites

Sorting in _FLTAR is slow, you might be better off sorting it separately after it gives you the file list.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Posted (edited)

I didn't  notice i was using the sorting switch, However i did put the test example before the _FLTAR  example where the dirs are needed to expanded first, the behavior may vary on different computers as we know it..

Still, arguably 40% faster here with my tests  ,I tried looking into the _FLTAR Function to maybe do the adjustments and test it myself, but the way the code  is written .. gets too complicated in my eyes so i had to let it go

Edit: Added some readability pointers to the loop

 

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

×
×
  • Create New...