;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       ReParseDir() 
;; 
;;ACTION         Parse and intelligently rewrite directory string, removing all "..\" references 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2014/12/16 
;; 
;;HISTORY        1.0  - 2014/12/16 - Initial Release 
;; 
;;SYNTAX         ReParseDir(DirPath) 
;; 
;;PARAMETERS     DirPath - REQUIRED - A string containing a directory path 
;; 
;;REMARKS        Locates a "..\" reference and removes it and the referenced parent path. 
;;               Runs recursively to flatten all references 
;; 
;;RETURNS        String with reformatted directory path. 
;;               The presence/absense of the final "\" is preserved in the reformatted string. 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K3, W2K8, W2K12 
;; 
;;EXAMPLES       $D = ReParseDir(@SCRIPTDIR + '..\Logs') 
;;               ; If @SCRIPTDIR returned C:\Program Files\Tools\Bin, the result in $D would be 
;;               ; C:\Program Files\Tools\Logs 
; 
;  
Function ReParseDir($_Dir)
 
  Dim $_P, $_I				; index pointers 
  Dim $_Tag				; process loop tag 
  Dim $_aTmp				; temp array 
  Dim $_D, $_T				; delimiter & Temp string 
  Dim $_L				; LastChar 
 
 
  $_L = ''
 
  If InStr($_Dir, '\..')
 
    If Right($_Dir, 1) = '\' $_L = '\' EndIf
 
    ; init vars 
    $_P = 0
    $_Tag = InStr($_Dir, '\..')		; must process if ".." is found 
 
    While($_Tag)
      $_aTmp = Split($_Dir, '\')
      If $_aTmp[$_P] = '..'
        ; Remove current and previous values 
        $_aTmp[$_P - 1] = ''
        $_aTmp[$_P]     = ''
 
        ; Reassemble the string without the directory pair 
        $_D = ''
        $_T = ''
        For $_I = 0 to UBound($_aTmp)
          If $_aTmp[$_I] <> ''
            $_T = $_T + $_D + $_aTmp[$_I]
            $_D = '\'
          EndIf
        Next
        $_Dir = $_T
        $_P = -1			; reset to begin again 
      EndIf
      $_P = $_P + 1			; increment pointer 
      $_Tag = InStr($_Dir, '\..')	; set process tag if ".." exists 
    Loop				; continue loop 
 
    $_Dir = $_Dir + $_L			; append trailing "\" if present in original 
 
  EndIf
 
  $ReParseDir = $_Dir			; return the dir string 
 
  Exit 0
 
EndFunction