Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0) Local $hSearch, $sFile, $sFileList, $asFileList[1] If Not FileExists($sPath) Then Return SetError(1, 1, "") If StringRegexp($sFilter, "[\\/:<>|]") Or (Not StringStripWS($sFilter, 8)) Then Return SetError(2, 2, "") If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2 Or $iFlag = 4 Or $iFlag = 5 Or $iFlag = 6) Then Return SetError(3, 3, "") If (StringMid($sPath, StringLen($sPath), 1) = "\") Then $sPath = StringTrimRight($sPath, 1); needed for Win98 for x:\ root dir $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 = 1 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop If $iFlag > 3 Then $sFile = $sPath & "\" & $sFile $sFileList &= $sFile & "|" WEnd FileClose($hSearch) $asFileList = StringSplit(StringTrimRight($sFileList, 1), "|") Return $asFileList EndFunc ;==>_FileListToArray
The gain of time isn't really significant (about 1,2 seconds for 260000 files) but if you use this function a lot, perhaps it can help you to gain few seconds.
Here is some tests results that I have done on my hardware.
Each test was doing 5 times (in a loop) and the result is the average of five values.
Here is the short description of the material that in served for the tests :
PC1 = Laptop IBM R60 C2D T5500 1.66 Ghz / 2.5Go Ram - Windows XP Pro SP3
PC2 = PC no name Quad Core Q6700 overcloked to 3Ghz / 4Go Ram - Windows Vista Pro SP1
Tests results :
PC1 test on 260000 files (stored on local drive) :
Original _FileListToArray = 5350 ms
Modified _FileListToArray = 4150 ms
PC1 test on 100000 files (stored on local drive) :
Original _FileListToArray = 2391 ms / 4075 ms (on battery)
Modified _FileListToArray = 1834 ms / 3150 ms (on battery)
PC1 test on 50000 files (stored on local drive) :
Original _FileListToArray = 1197 ms / 2002 ms (on battery)
Modified _FileListToArray = 924 ms / 1551 ms (on battery)
PC1 test on 50000 files (stored on NAS connected at 1Gb) :
Original _FileListToArray = 24578 ms
Modified _FileListToArray = 23843 ms
PC2 test on 100000 files (stored on local drive) :
Original _FileListToArray = 1307 ms
Modified _FileListToArray = 1091 ms
PC2 test on 50000 files (stored on local drive) :
Original _FileListToArray = 681 ms
Modified _FileListToArray = 575 ms
PC2 test on 50000 files (stored on NAS connected at 1Gb) :
Original _FileListToArray = 24635 ms
Modified _FileListToArray = 23447 ms
Edit :
There is a more great improvement on the last function, but it's for the newer version of AutoIt (Work time is divided by 4/5 for file search or folder search only).
I do an UDF with the too version but keep in mind that if you use only AutoIt > 3.3.1.0 then you can simplify to keep only the new version (that use @Extented from FileFindNextFile()).
; New version of FileListToArray (27/06/2009) ; #FUNCTION# ==================================================================================================== ================ ; Name...........: _FileListToArray ; Description ...: Lists files and\or folders in a specified path (Similar to using Dir with the /B Switch) ; Syntax.........: _FileListToArray($sPath[, $sFilter = "*"[, $iFlag = 0]]) ; Parameters ....: $sPath - Path to generate filelist for. ; $sFilter - Optional the filter to use, default is *. Search the Autoit3 helpfile for the word "WildCards" For details. ; $iFlag - Optional: specifies whether to return files folders or both ; |$iFlag=0(Default) Return both files and folders ; |$iFlag=1 Return files only ; |$iFlag=2 Return Folders only ; |$iFlag=4 Return Folder/File names also with full path ; Return values .: @Error - 1 = Path not found or invalid ; |2 = Invalid $sFilter ; |3 = Invalid $iFlag ; |4 = No File(s) Found ; Author ........: SolidSnake <MetalGX91 at GMail dot com> ; Modified.......: ; Remarks .......: 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 ; Related .......: ; Link ..........; ; Example .......; Yes ; ==================================================================================================== =========================== ;Special Thanks to Helge and Layer for help with the $iFlag update ;=============================================================================== Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0) If @AutoItVersion > "3.3.1.0" Then _FileListToArrayNew($sPath, $sFilter, $iFlag) Else _FileListToArrayOld($sPath, $sFilter, $iFlag) EndIf EndFunc ;==>_FileListToArray ; For versions of AutoIt > 3.3.1.0 (Use of @extented in FileFindNextFile() Func _FileListToArrayNew($sPath, $sFilter = "*", $iFlag = 0) Local $hSearch, $sFile, $sFileList, $sDelim = "|" If Not FileExists($sPath) Then Return SetError(1, 1, "") If StringRegExp($sFilter, "[\\/:<>|]") Or (Not StringStripWS($sFilter, 8)) Then Return SetError(2, 2, "") If StringRight($sPath, 1) <> "\" Then $sPath &= "\" If $iFlag > 3 Then $sDelim &= $sPath $iFlag -= 4 EndIf $hSearch = FileFindFirstFile($sPath & $sFilter) If $hSearch = -1 Then Return SetError(4, 4, "") EndIf Switch $iFlag Case 0; Files and Folders While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop EndIf $sFileList &= $sDelim & $sFile WEnd Case 1; Files Only While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop EndIf ; bypass folder If @extended Then ContinueLoop; This version for new beta $sFileList &= $sDelim & $sFile WEnd Case 2; Folders Only While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop EndIf ; bypass file If @extended = 0 Then ContinueLoop; This version for new beta $sFileList &= $sDelim & $sFile WEnd Case Else Return SetError(3, 3, "") EndSwitch FileClose($hSearch) Return StringSplit(StringTrimLeft($sFileList, 1), "|") EndFunc ;==>_FileListToArrayNew ; For versions of AutoIt < 3.3.1.1 Func _FileListToArrayOld($sPath, $sFilter = "*", $iFlag = 0) Local $hSearch, $sFile, $sFileList, $sDelim = "|" If Not FileExists($sPath) Then Return SetError(1, 1, "") If StringRegExp($sFilter, "[\\/:<>|]") Or (Not StringStripWS($sFilter, 8)) Then Return SetError(2, 2, "") If StringRight($sPath, 1) <> "\" Then $sPath &= "\" If $iFlag > 3 Then $sDelim &= $sPath $iFlag -= 4 EndIf $hSearch = FileFindFirstFile($sPath & $sFilter) If $hSearch = -1 Then Return SetError(4, 4, "") EndIf Switch $iFlag Case 0; Files and Folders While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop $sFileList &= $sDelim & $sFile WEnd Case 1; Files Only While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop ; bypass folder If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop $sFileList &= $sDelim & $sFile WEnd Case 2; Folders Only While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop ; bypass file If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop $sFileList &= $sDelim & $sFile WEnd Case Else Return SetError(3, 3, "") EndSwitch FileClose($hSearch) Return StringSplit(StringTrimLeft($sFileList, 1), "|") EndFunc ;==>_FileListToArrayOld
New Edit :
A new version with recursive search, multiple search, exclusion of files/directory is in progress (see the last posts).
Thanks to BaKaMu.
Edited by Tlem, 28 June 2009 - 08:19 AM.






