Opened 17 years ago
Closed 17 years ago
#1059 closed Bug (Fixed)
Incorrect error handling in _FileListToArray()
| Reported by: | Spiff59 | Owned by: | J-Paul Mesnage |
|---|---|---|---|
| Milestone: | 3.3.1.2 | Component: | AutoIt |
| Version: | 3.3.0.0 | Severity: | None |
| Keywords: | Cc: |
Description
FLTA is documented to return an @error = 4 condition when a call results in no matching files or folders. It is improperly coded and only returns the error when called in "Files and Folders" mode ($iFlag = 0). If $iFlag is set to either 1 or 2, and no matches are found, an array is incorrectly returned.
The following replacement function addresses this issue, and is on average 33% faster than the current 3.3.1.1 beta version. It is 100% compatible, and requires no modifications to the existing function header, nor to the function's helpfile entry.
This is the core logic of numerous FLTA development routines, and it has been undergoing user-testing in the field for about three months.
17-line function follows:
Func _FileListToArray2($sPath, $sFilter = "*", $iFlag = 0) Local $hSearch, $sFile, $sFileList, $sDelim = "|" $sPath = StringRegExpReplace($sPath, "[\\/]+\z", "") & "\" ; ensure single trailing backslash If Not FileExists($sPath) Then Return SetError(1, 1, "") If StringRegExp($sFilter, "[\\/:><\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "") If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "") $hSearch = FileFindFirstFile($sPath & $sFilter) If @error Then Return SetError(4, 4, "") While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop If $iFlag = 1 And @extended Then ContinueLoop ; bypass folder If $iFlag = 2 And @extended = 0 Then ContinueLoop ; bypass file $sFileList &= $sDelim & $sFile WEnd FileClose($hSearch) If Not $sFileList Then Return SetError(4, 4, "") Return StringSplit(StringTrimLeft($sFileList, 1), "|") EndFunc;==>_FileListToArray
Attachments (0)
Change History (3)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
Revision: The possible combinations of $iFlag and @extended are 0,0],[0,1],[1,0][1,1],[2,0],[2,1. The test to exclude items in the main While loop only triggers ContinueLoop for two combinations: [2,0] and [1,1]. I prefer the consolidation of the two tests into a single line like below. It produces a measurable increase in performance and reduces the function size to 18 lines.
Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0) Local $hSearch, $sFile, $sFileList, $sDelim = "|" $sPath = StringRegExpReplace($sPath, "[\\/]+\z", "") & "\" ; ensure single trailing backslash If Not FileExists($sPath) Then Return SetError(1, 1, "") If StringRegExp($sFilter, "[\\/:><\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "") If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "") $hSearch = FileFindFirstFile($sPath & $sFilter) If @error Then Return SetError(4, 4, "") While 1 $sFile = FileFindNextFile($hSearch) If @error Then ExitLoop If ($iFlag + @extended = 2) Then ContinueLoop $sFileList &= $sDelim & $sFile WEnd FileClose($hSearch) If Not $sFileList Then Return SetError(4, 4, "") Return StringSplit(StringTrimLeft($sFileList, 1), "|") EndFunc;==>_FileListToArray
comment:3 by , 17 years ago
| Milestone: | → 3.3.1.2 |
|---|---|
| Owner: | set to |
| Resolution: | → Fixed |
| Status: | new → closed |
Fixed in version: 3.3.1.2

Ok, when I counted it in SciTE, I counted the lines between Func and EndFunc.
It is 19 lines in length, total.