Modify

Opened 15 years ago

Closed 15 years ago

#1059 closed Bug (Fixed)

Incorrect error handling in _FileListToArray()

Reported by: Spiff59 Owned by: Jpm
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 Changed 15 years ago by Spiff59

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

comment:2 Changed 15 years ago by Spiff59

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 Changed 15 years ago by Jpm

  • Milestone set to 3.3.1.2
  • Owner set to Jpm
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed in version: 3.3.1.2

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain Jpm.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.