Sign in to follow this  
Followers 0
mrbond007

_FileListToArray

2 posts in this topic

had to use the original _filelisttoarray but it was so slow i decided to change it

;===============================================================================
;
; Description:      lists all files and folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax:           _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
; Parameter(s):     $sPath = Path to generate filelist for
;                  $iFlag = determines weather to return file or folders or both
;                   $sFilter = The filter to use. Search the Autoit3 manual for the word "WildCards" For details
;                           $iFlag=0(Default) Return both files and folders
;                           $iFlag=1 Return both files and folders with full path
;                           $iFlag=2 Return Files Only
;                           $iFlag=3 Return Files Only with full path
;                           $iFlag=4 Return Folders Only
;                           $iFlag=5 Return Folders Only with full path
;
; Requirement(s):   None
; Return Value(s):  On Success - Returns an array containing the list of files and folders in the specified path
;                       On Failure - Returns the an empty string "" if no files are found and sets @Error on errors
;                       @Error=1 Path not found or invalid
;                       @Error=2 Invalid $sFilter
;                       @Error=3 Invalid $iFlag
;                       @Error=4 No File(s) Found
;
; Author(s):        SolidSnake <MetalGX91 at GMail dot com> (Modified by mrbond007)
; Note(s):          The array returned is one-dimensional and is made up as follows:
;                   $array[0] = Number of Files\Folders returned
;                   $array[1] = 1st File\Folder
;                   $array[2] = 2nd File\Folder
;                   $array[3] = 3rd File\Folder
;                   $array[n] = nth File\Folder
;
;                   Special Thanks to Helge and Layer for help with the $iFlag update
;===============================================================================
Func _FileListToArray($sPath, $sFilter, $iFlag)
    Local $hSearch = "", $sFile = 0, $asFileList = ""
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2 Or $iFlag = 3 Or $iFlag = 4 Or $iFlag = 5) Then Return SetError(3, 3, "")
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch = -1 Then Return SetError(4, 4, "")
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then
            SetError(0)
            ExitLoop
        EndIf
        If $iFlag = 0 Then $asFileList = $asFileList & $sFile & "|"
        If $iFlag = 1 Then $asFileList = $asFileList & $sPath & "\" & $sFile & "|"
        If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then $asFileList = $asFileList & $sFile & "|"
        If $iFlag = 3 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then $asFileList = $asFileList & $sPath & "\" & $sFile & "|"
        If $iFlag = 4 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then $asFileList = $asFileList & $sFile & "|"
        If $iFlag = 5 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then $asFileList = $asFileList & $sPath & "\" & $sFile & "|"
    WEnd
    FileClose($hSearch)
    Return StringSplit(StringTrimRight($asFileList, 1) , "|")
EndFunc

examples :

#Include <Array.au3>
$fing = _FileListToArray(@SystemDir, "*", 0)
_ArrayDisplay($fing, $fing[0] & " Elements found")

$fing = _FileListToArray(@SystemDir, "*", 1)
For $i = 1 To $fing[0]
    MsgBox(0, "", $fing[$i])
Next

some results on my pc (2502 files):

$iFlag = 0 : 0.492 mseconds

$iFlag = 1 : 1.982 mseconds

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