;;=====================================================================================----- 
;FUNCTION      DirList() 
; 
;ACTION        Returns an array with a list of files in a given directory 
; 
;AUTHOR        Jens Meyer 
;              'g' versions were modified by Glenn Barnas 
; 
;VERSION       1.3g 
; 
;HISTORY       1.0  - 2002/01/18 - Initial release 
;	       1.2  - 2007/02/22 - Fixes to recursion 
;              1.3g - 2008/01/20 - Adaptation to handle 4-char file extensions, which Kix 
;                                  doesn't seem to process properly. Add a "?" or "*" to 
;                                  the end of the filespec to use default processing. 
;               
;SYNTAX        DirList(DirName [, Options]) 
; 
;PARAMETERS    DirName - Required - String 
;              contains the directory pathspec value 
; 
;              Options - Optional - Integer 
;              Value for additional options, options are set bitwise 
;              1 = include dirnames (denoted by a backslash) that match the search mask 
;              2 = include the full path in the returned data 
;              4 = recursively search all subdirectories 
; 
;RETURNS       array with a list of files, otherwise an empty array 
; 
;REMARKS       none 
; 
;DEPENDENCIES  none 
; 
;EXAMPLE       $dirlist = DIRLIST("c:\*.*",1 + 2 + 4) 
; 
Function DirList($_DirName, optional $_Options)
 
  Dim $_FileName			;  
  Dim $_Counter				;  
  Dim $_Mask				; filespec search mask 
  Dim $_List				;  
  Dim $_SubList				;  
  Dim $_SubCounter			;  
  Dim $_Exact				; exact-match flag 
 
  $_Counter = -1
 
  $_DirName = trim($_DirName)		; trim spaces 
  If $_DirName = ''			; default to current dir 
    $_DirName = @CURDIR
  EndIf
 
  If Right($_DirName, 1)='\'		; Remove any trailing slash 
    $_DirName = Left($_DirName, Len($_DirName) - 1)
  EndIf
 
  If GetFileAttr($_DirName) & 16		; default mask is "*.*" if dir was specified 
    $_Mask='*.*'
  Else					; otherwise, get the specified mask 
    $_Mask = SubStr($_DirName, InStrRev($_DirName, '\') + 1)
    $_DirName = Left($_DirName, Len($_DirName ) - Len($_Mask) - 1)
    $_Exact = Right($_Mask, Len($_Mask) - InStrRev($_Mask, '*'))
    If InStr($_Exact, '?') $_Exact = '' EndIf
  EndIf
 
  ReDim $_List[10]
 
  ; start the directory enumeration 
  $_FileName = Dir($_DirName + '\' + $_Mask)
 
  While $_FileName <> '' And Not @ERROR
 
    If $_Exact And Right($_Filename, Len($_Exact)) <> $_Exact
      $_FileName = '.'
    EndIf
    If $_FileName <> '.' And $_FileName <> '..'
      Select
 
      ; process directories 
      Case (GetFileAttr($_DirName + '\' + $_FileName) & 16)
        If $_Options & 1			; are directories selected? 
          $_Counter = $_Counter + 1	; add the folder to the list 
 
          If $_Options & 2		; include full path? 
            $_List[$_Counter] = $_DirName + '\' + $_FileName + '\'
          Else
            $_List[$_Counter] = $_FileName + '\'
          EndIf
 
        EndIf
 
        ; if directory recursion is selected, get the subfolders in the current folder 
        If ($_Options & 4)
          $_SubList = dirlist($_DirName + '\' + $_FileName + '\' + $_Mask, $_Options & 5)
 
          ; add the subfolder contents to the array 
          If UBound($_SubList) + 1
            Redim Preserve $_List[UBound($_List) + UBound($_SubList) + 1]
            For $_SubCounter = 0 To UBound($_SubList)
              $_Counter = $_Counter + 1
              If $_Options & 2
                $_List[$_Counter] = $_DirName + '\' + $_FileName + '\' + $_SubList[$_SubCounter]
              Else
                $_List[$_Counter] = $_FileName + '\' + $_SubList[$_SubCounter]
              EndIf
            Next
          EndIf
        EndIf
 
      Case ($_Options & 2)
        $_Counter = $_Counter + 1
        $_List[$_Counter] = $_DirName + '\' + $_FileName
 
      Case 1
        $_Counter = $_Counter + 1
        $_List[$_Counter] = $_FileName
 
      EndSelect
 
      If $_Counter mod 10
        Redim Preserve $_List[$_Counter + 10]
      EndIf
 
    EndIf
 
    $_FileName = dir('')
 
  loop
 
  If $_Counter + 1
    Redim Preserve $_List[$_Counter]
  Else
    $_List = ''
  EndIf
 
  If $_Mask <> '*.*' and ($_Options & 4)
    $_FileName = Dir($_DirName + '\*.*')
 
    While $_FileName <> '' and @ERROR = 0
      If $_FileName <> '.' and $_FileName <> '..'
        If (getfileattr($_DirName + '\' + $_FileName) & 16)
          $_SubList = DirList($_DirName + '\' + $_FileName + '\' + $_Mask, $_Options & 5)
          If UBound($_SubList) + 1
            Redim Preserve $_List[UBound($_List) + UBound($_SubList) + 1]
            For $_SubCounter = 0 To UBound($_SubList)
              $_Counter = $_Counter + 1
 
              If $_Options & 2
                $_List[$_Counter] = $_DirName + '\' + $_FileName + '\' + $_SubList[$_SubCounter]
              Else
                $_List[$_Counter] = $_FileName + '\' + $_SubList[$_SubCounter]
              EndIf
 
            Next
 
          EndIf
        EndIf
      EndIf
 
      $_FileName = Dir('')
 
    Loop
 
  EndIf
 
  If $_Counter + 1
    Redim Preserve $_List[$_Counter]
  Else
    $_List = ''
  EndIf
 
  $DirList = $_List
 
EndFunction