#include-once
; #INDEX# =======================================================================================================================
; Title .........: File; should be added
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_FileListTreeToArray
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _FileListTreeToArray
; Description ...: List all files from the wished directiry with all files from all subfolders to an array.
; Syntax.........: _FileListTreeToArray($sPath[, $fRelation = 1[, $sType = ""[,$sSort = "N"]]])
; Parameters ....: $sPath - Path to the directory that has to be listed
;                  $fRelation - Optional: Path return type, "0" = absolute (default), "1" = relative
;                  $sType - Optional: Search type, see helptopic "FileGetAttrib"; possible : "RASHD", with "-" before for negotation:
;                  |R - Readonly
;                  |A - Archive
;                  |S - System
;                  |H - Hidden
;                  |D - Directory
;                  $sSort - Optional: Sort results, see "cmd.exe /K dir /?" at subpoint "order", with "-" before for negotation: possibilities:
;                  |N - Name, alphabetically
;                  |S - Size, smaller files first
;                  |E - Extension, alphabetically
;                  |D - Date/Time, older files first
;                  |G - Directories first
; Return values .: Success - Array with $array[0] = Number of items, $array[n] = path of items
;				   Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
;                  |1 - Invalid $sPath
;                  |2 - Invalid $fRelation
;                  |3 - Invalid $sType
;                  |4 - Invalid $sSort
;                  |5 - User32.dll failure
; Author ........: Jonas Neef
; Remarks .......: Caution: This function can take very long! Wildcards are supported.
; Related .......: FileGetAttrib, FileSetAttrib, _FileListToArray
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _FileListTreeToArray($sPath, $fRelation = 0, $sType = "", $sSort = "N")
	Local $aCheck, $vStdout, $sTemp, $sPlaceholder, $sTempSort, $vStdout, $sLine, $iCounter, $aReturn
	; Check parameters, validate path
	$aCheck = StringLeft($sPath, 3)
	$aCheck = StringSplit($aCheck, ":")
	If $aCheck[0] <> 2 And StringLeft($sPath, 1) <> "\" Then $sPath = @WorkingDir & "\" & $sPath
	If $aCheck[0] <> 2 And StringLeft($sPath, 1) = "\" Then $sPath = @WorkingDir & $sPath
	If StringRight($sPath, 1) = "\" Then $sPath = StringTrimRight($sPath, 1)
	If Not FileExists($sPath) Then Return SetError(1, 0, 0)
	If $fRelation > 1 Or $fRelation < 0 Then Return SetError(2, 0, 0)
	$sTempSort = StringReplace($sSort, "-", "")
	If StringLen($sTempSort) <> 1 Or _
			$sTempSort <> "N" And _
			$sTempSort <> "S" And _
			$sTempSort <> "E" And _
			$sTempSort <> "D" And _
			$sTempSort <> "G" And _
			$sTempSort <> "" Then _
			Return SetError(3, 0, 0)
	If StringReplace( _
			StringReplace( _
			StringReplace( _
			StringReplace( _
			StringReplace( _
			StringReplace($sType, "-", "") _
			, "H", "") _
			, "S", ""), _
			"A", ""), _
			"R", ""), _
			"D", "") <> "" Then _
			Return SetError(4, 0, 0)
	; Start CMD to load the tree
	If $sType = "" Then
		$vStdout = Run(@ComSpec & ' /C cd ..\..\..\..\.. & dir "' & $sPath & '" /A /B /O:' & $sSort & ' /S', @WorkingDir, @SW_HIDE, 6)
	Else
		$vStdout = Run(@ComSpec & ' /C cd ..\..\..\..\.. & dir "' & $sPath & '" /A:' & $sType & ' /B /O:' & $sSort & ' /S', @WorkingDir, @SW_HIDE, 6)
	EndIf
	; Get data via Stdout
	While 1
		$sLine = StdoutRead($vStdout)
		If @error Then ExitLoop
		If $sLine <> "" Then $sTemp &= $sLine
	WEnd
	; Create Placeholder for hole string
	For $iCounter = 0 To StringLen($sTemp)
		$sPlaceholder &= "  "
	Next
	; Convert string from extended Ascii to Ansi
	$sTemp = DllCall("user32.dll", "long", "OemToChar", "str", $sTemp, "str", $sPlaceholder)
	$sPlaceholder = ""
	If Not IsArray($sTemp) Or $sTemp[0] < 1 Then Return SetError(5, 0, 0)
	$sTemp = $sTemp[2]
	; Format finally
	$sTemp = StringReplace($sTemp, @CRLF, @LF)
	$sTemp = StringReplace($sTemp, @CR, @LF)
	If StringRight($sTemp, 1) = @LF Then $sTemp = StringTrimRight($sTemp, 1)
	If StringLeft($sTemp, 1) = @LF Then $sTemp = StringTrimLeft($sTemp, 1)
	If Not StringInStr($sTemp, @LF) Then
		If $sTemp = "" Then
			Local $aReturn[1] = [0]
		Else
			Local $aReturn[2] = [1, $sTemp]
		EndIf
	Else
		Local $aReturn = StringSplit($sTemp, @LF)
	EndIf
	If Not IsArray($aReturn) Then
		Return SetError(5, 0, 0)
	EndIf
	; If relative path is chosen edit the paths
	If $fRelation = 1 Then
		For $iCounter = 1 To $aReturn[0] Step 1
			$aReturn[$iCounter] = StringReplace($aReturn[$iCounter], $sPath & "\", "")
		Next
	EndIf
	; Return array
	Return SetError(0, 0, $aReturn)
EndFunc   ;==>_FileListTreeToArray