;;=====================================================================================----- 
;Function       DirPlus() 
; 
;Action         Returns an array containing directory files and folders 
; 
;Author         Bryce Lindsay 
; 
;Version        2.34 - 2005/09/20 
; 
;History        1.0  - 2001/11/26 - Initial Release 
;               2.0  - 2004/09/12 - Rewrite - Mask option deprecated, Added multiple search patterns 
;               2.34 - 2005/09/20 - Fixed the file/folder option "d" to be non language dependent, thanks Jochen 
; 
;Syntax         DirPlus(Path [,Options]) 
; 
;Parameters     Path - Required - String 
;                  Full path To To a folder that you want To Return information on. 
;                  "c:\program files" 
; 
;               OPTIONS 
;                  /S - Displays files In specified directory and all subdirectories. 
;                                Use a /S# where # is equal to the subfolder depth that you want to recurse. 
; 
;                  /A - Displays files with specified attributes: 
;                   D  Directories 
;                   R  Read-only files 
;                   H  Hidden files 
;                   A  Files ready For archiving 
;                   S  System files 
;                   -  Prefix meaning not 
; 
;                  /M - Apply mask string To filter based on InSTR(), separate Each search string witha a | 
; 
;                  /F - Return a given File extension like exe log or txt Seperate each extension type with a space 
; 
; 
;Remarks        Finaly fixed this UDF For To handle multiple recursions, 
;               also should have a faster responce time since it is using the FSO 
; 
;               ***Please note that the syntax For version 2.0 of this UDF has changed.*** 
;               made some tweaks using feedback from Les! thanks!  Also NTDOC! 
; 
;Returns        Array of FSO Objects 
;               Returns and array of FSO objects that are equal the file and folder objects of 
;               the given path.  Also returns a @ERROR code For event handling. 
; 
;Dependencies   FSO 
; 
;Example(s)     $Dir = dirplus("c:\program files") ;returns all files and folders In the "c:\program files" folder 
;               $Dir = dirplus("c:\program files","/s") ;all fiels and folders including subfolders 
;               $Dir = dirplus("c:\","/a-d") ;returns only a list of files In the c:\ 
;               $Dir = dirplus("c:\","/ad") ;returns only a list of folders In the c:\ 
;               $Dir = dirplus("c:\program files","/ad /s") ;returns only the folders including all subfolders. 
; 
;               $Dir = dirplus("g:\kix\udf","/s /ad /m dir") ; recursive subfolder search, folders only, using a mask string of "dir" 
; 
;               $desktop = dirplus("%userprofile%\desktop","/a-d") 
;               For Each $file In $desktop 
;                 ? $file 
;                 ? $file.size 
;               Next 
; 
Function DirPlus($_Path, Optional $_Options, Optional $_F, Optional $_Sfflag)
 
  If not vartype($_F)
    Dim $_F
  EndIf
  
  If not vartype($_Sfflag)
    Dim $_Sfflag  
  EndIf
 
  Dim $_File, $_I, $_Temp, $_Item, $_Ex1
  Dim $_Mask, $_Mask1 $_MaskArray, $_Maskarray1
  Dim $_Ex2, $_Code, $_CodeWeight, $_TargetWeight, $_Weight, $_Masktrue
  Dim $_Tarray[0]
 
  ; Define null values to eliminate KGen warnings 
  $_Mask         = ''
  $_Mask1        = ''
  $_TargetWeight = 0
  $_Weight       = 0
  $_MaskTrue     = 0
 
  $_Ex1 = SetOption('Explicit', 'On')
  $_Ex2 = SetOption('NoVarsInStrings', 'On')
  $_CodeWeight = 0
 
  If not Exist($_Path) 
    $_Temp = SetOption(Explicit,$_Ex1)
    $_Temp = SetOption(NoVarsInStrings,$_Ex2)
    Exit @ERROR
  EndIf
 
  If not vartype($_F)
    $_F = CreateObject('Scripting.FileSystemObject').getfolder($_Path)
  EndIf
  If @ERROR 
    $_Temp = SetOption(Explicit, $_Ex1)
    $_Temp = SetOption(NoVarsInStrings, $_Ex2)
    Exit @ERROR
  EndIf
 
   For Each $_Temp In Split($_Options, '/')
    $_Temp=Trim($_Temp)
    Select
     Case left($_Temp,1) = 's'
      If not vartype($_Sfflag)
        If Val(right($_Temp,-1)) = 0
          $_Sfflag = -1
        Else
          $_Sfflag = Val(right($_Temp,-1))
        EndIf
      EndIf
     Case Left($_Temp,1) = 'a'
      Select
       Case Right($_Temp,-1) = 'd'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If $_File.attributes & 16 '                   ;If $_File.type = "File Folder" 
       Case Right($_Temp,-1) = '-d'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If ($_File.attributes & 16) = 0 '             ; If $_File.type <> "File Folder" 
       Case Right($_Temp,-1)='s'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If $_File.attributes & 4 '
       Case Right($_Temp,-1) = '-s'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If ($_File.attributes & 4) = 0 '
       Case Right($_Temp,-1) = 'h'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If $_File.attributes & 2 '
       Case Right($_Temp,-1) = '-h'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If ($_File.attributes & 2) = 0 '
       Case Right($_Temp,-1) = 'r'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If $_File.attributes & 1 '
       Case Right($_Temp,-1) = '-r'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If ($_File.attributes & 1) = 0 '
       Case Right($_Temp,-1) = 'a'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If $_File.attributes & 32 '
       Case Right($_Temp,-1) = '-a'
        $_CodeWeight = $_CodeWeight + 1
;KGEN:IgnoreNext 
        $_Temp = 'If ($_File.attributes & 32) = 0 '
      EndSelect
;KGEN:IgnoreNext 
      $_Code = $_Temp + '$_Weight=$_Weight+1 EndIf' +@CRLF + $_Code
 
     Case Left($_Temp,1) = 'm'
      $_Maskarray = Split(Right($_Temp,-2), '|')
      $_Codeweight = $_Codeweight + 1
;KGEN:IgnoreNext 
      $_Code = '$_Masktrue=0 For Each $_Mask In $_Maskarray If InStr($_File.name,$_Mask) $_Masktrue=1 EndIf Next If $_Masktrue $_Weight=$_Weight+1 EndIf' + @CRLF +$_Code
     Case Left($_Temp,1) = 'f'
      $_Maskarray1 = Split(Right($_Temp,-2),' ')
      $_Codeweight = $_Codeweight + 1
;KGEN:IgnoreNext 
      $_Code = '$_Masktrue=0 For Each $_Mask1 In $_Maskarray1 If SubStr($_File.name, InStrRev($_File.name, '.') + 1) = $_Mask1 $_Masktrue=1 EndIf Next If $_Masktrue $_Weight = $_Weight + 1 EndIf' + @CRLF +$_Code
 
    EndSelect
  Next
;KGEN:IgnoreNext 
  $_Code = '$_Weight = 0 $_TargetWeight = ' + $_Codeweight + @CRLF + $_Code
;KGEN:IgnoreNext 
  $_Code = $_Code + 'If $_Weight = $_Targetweight Exit 1 EndIf'
 
  For Each $_File In $_F.subfolders
    If Execute($_Code)
      $_Tarray[$_I] = $_File
      $_I = $_I + 1
      ReDim Preserve $_Tarray[$_I]
    EndIf
    If $_Sfflag
;'Recursion: ' $_File ', ' $_Options ', ' $_File ', ' $_Sfflag-1 ? 
      $_Temp = DirPlus($_File, $_Options, $_File, $_Sfflag-1)
      For Each $_Item In $_Temp
        $_Tarray[$_I] = $_Item
        $_I = $_I + 1
        ReDim Preserve $_Tarray[$_I]
      Next
    EndIf
  Next
  For Each $_File In $_F.files
    If Execute($_Code)
      $_Tarray[$_I] = $_File
      $_I = $_I + 1
 
      ReDim Preserve $_Tarray[$_I]
    EndIf
  Next
 
  If $_I
    ReDim Preserve $_Tarray[$_I - 1]
    $_I = 0
  Else
    $_Tarray = 0
  EndIf
 
  $DirPlus = $_Tarray
  $_Temp = SetOption(Explicit, $_Ex1)
  $_Temp = SetOption(NoVarsInStrings, $_Ex2)
  
  Exit @ERROR
 
EndFunction