Code : Tout sélectionner
; #FUNCTION# ================================================================================================
; Name: _FileListToArrayEx2Bis
; Description: full compatible _FileListToArray replacement (with more speed and additional features)
; additional: multi-filter, recursiv search
; optional full pathname
; Syntax: _FileListToArrayEx2Bis([$sPath = @ScriptDir, [$sFilter = "*", [$iFlag = 0, [$iFormat = 1]]]]]])
; Parameter(s): $sPath = optional: path to generate filelist for, multi paths separated with semicolon (ex: "C:\";D:\Temp")
; if no path is given then @ScriptDir is used
; $sFilter = optional: The filter to use. (default: "*")
; multi filters separated with semicolon (ex: *.exe; *.txt will find all .exe and .txt files)
; (Search the Autoit3 manual for the word "WildCards" for details)
; $iFlag = Optional: specifies whether to return files, folders or both
; 0 = (Default) Return both files and folders
; 1 = Return files only
; 2 = Return folders only
; $iFlag + 4 = Return Filenames and/or Folders incl full Path
; $iFormat = optional: return format
; 0 = String ( "|" delimited)
; 1 = (default) one-dimensional array, array[0] = number of files\folders returned
; 2 = one-dimensional array, 0-based
; Requirement(s): none
; Return Value(s): on success: string or array (dependent on $iFormat)
; Author(s): bernd670, Tlem, Spiff59, Zedna, KaFu, SmOke_N, GEOSoft, BaKaMu
; =================================================================================================
Func _FileListToArrayEx2Bis($sPath = @ScriptDir, $sFilter = "*", $iFlag = 0, $iFormat = 1)
Local $hSearch, $iPCount, $iFCount, $sFile, $TFlag = 0, $sFileList = "", $sTExclude = ""
If $sPath = -1 Or $sPath = Default Then $sPath = @ScriptDir
If $sFilter = -1 Or $sFilter = Default Then $sFilter = "*"
If $iFlag = -1 Or $iFlag = Default Then $iFlag = 0
If $iFormat = -1 Or $iFormat = Default Then $iFormat = 1
;separate multi filter
Local $aFilter = StringSplit($sFilter, ';')
Local $sDelim = "|" ;reset $sDelim
Local $sPathItem = StringStripWS($sPath, 3) ;Strip leading and trailing spaces
If StringRight($sPathItem, 1) <> "\" Then $sPathItem = $sPathItem & "\" ;check for trailing "\"
;return full-path or filename only ($iFlag 0,1,2 = Filename, $iFlag 4,5,6 = full path)
If $iFlag > 3 Then
$sDelim &= $sPathItem
$TFlag = $iFlag - 4
Else
$TFlag = $iFlag
EndIf
;perform the search
For $iFCount = 1 To $aFilter[0]
Local $FilterItem = StringStripWS($aFilter[$iFCount], 3) ;Strip leading and trailing spaces
If StringRegExp($FilterItem, "[\\/:<>|]") Then ContinueLoop ;Look for bad chars
$hSearch = FileFindFirstFile($sPathItem & $FilterItem)
If @error Then ContinueLoop
Switch $TFlag
Case 0 ;Files and Folders
While True
$sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
;optional exclude filename/folder
If $sTExclude Then
If StringRegExp(StringRegExpReplace($sFile, "(.*?[\\/]+)*(.*?\z)", "\2"), "(?i)\A" & $sTExclude & "\z") Then ContinueLoop
EndIf
$sFileList &= $sDelim & $sFile
WEnd
Case 1 ;Files Only
While True
$sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
;If StringInStr(FileGetAttrib($sPath & $sFile), "D") <> 0 Then ContinueLoop; bypass folder (ver 3.3.0.0)
If @extended Then ContinueLoop ;bypass folder (for Autoit versions > 3.3.0.0)
;optional exclude filename/folder
If $sTExclude Then
If StringRegExp(StringRegExpReplace($sFile, "(.*?[\\/]+)*(.*?\z)", "\2"), "(?i)\A" & $sTExclude & "\z") Then ContinueLoop
EndIf
$sFileList &= $sDelim & $sFile
WEnd
Case 2 ;Folders Only
While True
$sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
;If StringInStr(FileGetAttrib($sPath & $sFile), "D") = 0 Then ContinueLoop; bypass file (ver 3.3.0.0)
If @extended = 0 Then ContinueLoop ;bypass file (for Autoit versions > 3.3.0.0)
;optional exclude filename/folder
If $sTExclude Then
If StringRegExp(StringRegExpReplace($sFile, "(.*?[\\/]+)*(.*?\z)", "\2"), "(?i)\A" & $sTExclude & "\z") Then ContinueLoop
EndIf
$sFileList &= $sDelim & $sFile
WEnd
Case Else
Return SetError(3, 3, "")
EndSwitch
FileClose($hSearch)
Next
;---------------
;Do a recursive search
$hSearch = FileFindFirstFile($sPathItem & "*.*")
If Not @error Then
While True
$sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
;If StringInStr(FileGetAttrib($sPath & $sFile), "D") = 0 Then ContinueLoop; bypass file (ver 3.3.0.0)
If @extended = 0 Then ContinueLoop ;bypass file (for Autoit versions > 3.3.0.0)
;optional exclude filename/folder
If $sTExclude Then
If StringRegExp(StringRegExpReplace($sFile, "(.*?[\\/]+)*(.*?\z)", "\2"), "(?i)\A" & $sTExclude & "\z") Then ContinueLoop
EndIf
;call recursive search
$sFileList &= _FileListToArrayEx2Bis($sPathItem & $sFile, $sFilter, $iFlag, 0)
WEnd
FileClose($hSearch)
EndIf
;---------------
;Set according return value
Switch $iFormat
Case 0
Return $sFileList
Case 1
If $sFileList = "" Then
Local $aRet[1] = [0]
Return $aRet
Else
Return StringSplit(StringTrimLeft($sFileList, 1), "|", $iFormat)
EndIf
Case 2
If $sFileList = "" Then
Return ""
Else
Return StringSplit(StringTrimLeft($sFileList, 1), "|", $iFormat)
EndIf
EndSwitch
EndFunc ;==>_FileListToArrayEx2Bis