;; 
;;=====================================================================================----- 
;; 
;;FUNCTION	 PathInfo() 
;; 
;;ACTION	 Returns information about the System PATH 
;; 
;;AUTHOR	 Glenn Barnas 
;; 
;;VERSION	 2.0  - 2009/03/12 
;; 
;;HISTORY        1.0  - 2003/04/18 - Initial Release 
;;               2.0  - 2009/03/12 - improved local processing, made return array a 
;;                                   consistent format array of arrays instead of a 
;;                                   mixed array format. 
;; 
;;SYNTAX	 PathInfo([host]) 
;; 
;;PARAMETERS	 Host - OPTIONAL - String 
;;               - Name of host to query 
;; 
;;REMARKS        Returns information about the system PATH for use in optimization and  
;;               problem checking. It is used by the PathTune application and similar 
;;               tools. For example, PathTune uses this information to report on 
;;               problems in the path, such as duplicate entries, path elementss that 
;;               don't exist, or elements that aren't rooted. This function removes dups, 
;;               non-existant, and invalid elements when the path groups are created.  
;; 
;;               Path Groups - path elements grouped by function 
;;               Admin	- User defined admin tool elements 
;;               System	- Anything within SYSTEMROOT 
;;               Apps1	- Any element in SYSTEMDRIVE:\Program Files 
;;               Apps2	- Everything else 
;;               Java	- Any element that references Java 
;; 
;;               The $_Tools array should be customized before use of this function! 
;; 
;;RETURNS        Array of Arrays 
;;               0 - Array of Current path elements 
;;               1 - Array of distinct path element groups: Admin, System, Java, Apps1, Apps2 
;;               2 - Array of error counts - Duplicate, Missing, Invalid 
;;               3 - Array of missing/invalid path element warning messages 
;; 
;;DEPENDENCIES	None 
;; 
;;TESTED WITH	W2k, W2K3, W2K8, XP, Vista 
;; 
;;EXAMPLES      None 
;; 
; 
Function PathInfo(OPTIONAL $_Target)
 
  Dim $_
  Dim $_Tools					; array of admin tool locations 
  Dim $_SPath					; Source Path 
  Dim $_aPath, $_Dir				; array of path segments, Enumerator 
  Dim $_aTmp[3]					; Array of data to return 
  Dim $_UNCPath, $_NPath			; UNC and Normalized path elements 
  Dim $_SRoot, $_SDrv				; system root and drive values 
  Dim $_Admin, $_Af				; Admin tools paths, AdminFlag 
  Dim $_System					; system paths 
  Dim $_Java					; Java paths 
  Dim $_Apps1, $_Apps2				; path to primary/secondary apps 
  Dim $_Dup, $_Missing, $_Invalid		; counters for bad elements 
 
 
  ; ====================================================================== 
  ; Array of folders considered Admin tools 
  ; This array must be customized! - similar paths must be listed most to least specific! 
  $_Tools = 'itcg\bin', 'local\bin', 'local', 'scripts', 'batch', 'cygwin'
  ; ====================================================================== 
 
 
  ; Initialize the error counters 
  $_Dup = 0
  $_Missing = 0
  $_Invalid = 0
 
  ; format the target name if defined 
  $_Target = IIf(CStr($_Target) <> '', '\\' + Join(Split(CStr($_Target), '\'), '', 3) + '\', '')
 
  ; Get the PATH & SYSTEMROOT values from the registry of the target system 
  $_SPath = ReadValue($_Target + 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'PATH')
  If @ERROR Exit @ERROR EndIf
  $_SRoot = ReadValue($_Target + 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'SystemRoot')
  If @ERROR Exit @ERROR EndIf
  $_SDrv = Left($_SRoot, 2)
 
  ; split the path segments into an array 
  $_aPath = Split($_SPath, ';')
 
  ; enumerate the path elements, eliminate duplicates, verify paths, and classify 
  For Each $_Dir in $_aPath
 
    ; remove trailing "\" 
    If Right($_Dir, 1) = '\' $_Dir = Left($_Dir, Len($_Dir) - 1) EndIf
 
    ; Create Normalized Path element with expanded environment vars 
    $_NPath = Join(Split($_Dir, '%%SYSTEMROOT%%'), $_SRoot)
    $_NPath = Join(Split($_NPath, '%%SYSTEMDRIVE%%'), $_SDrv)
    ; convert normalized path to UNC path for folder validation 
    If $_Target
      $_UNCPath = $_Target + Join(Split($_NPath, ':'), Chr(36))
    Else
      $_UNCPath = $_NPath
    EndIf
 
    ; PERFORM FOLDER ELEMENT VALIDATION 
    ; NPV must begin with a drive letter to be a valid, rooted path element 
    ; UNC must exist to be a valid element 
    If Not InStr($_NPath, '%%')		; ignore unresolved env vars 
      $_ = Exist($_UNCPath)
      If @ERROR
          $_aTmp[3] = $_aTmp[3] + 'Unverified Folder: ' + $_Dir + ' : ' + @ERROR + @CRLF
      Else
        If Not $_
          $_aTmp[3] = $_aTmp[3] + 'Missing Folder: ' + $_Dir + @CRLF
          $_Missing = $_Missing + 1
          $_Dir = ''
        EndIf
      EndIf
      If Not InStr($_NPath, ':')
        $_aTmp[3] = $_aTmp[3] + 'Invalid Folder: ' + $_Dir + @CRLF
        $_Invalid = $_Invalid + 1
        $_Dir = ''
      EndIF
    EndIf
 
    ; CHECK AND CLASSIFY PATH ELEMENT 
    $_Af = 0
    For Each $_ in $_Tools
      If InStr($_NPath, $_) And Not $_Af			; ADMIN PATH ELEMENT 
        If InStr($_Admin, $_Dir + ';')
          $_aTmp[3] = $_aTmp[3] + 'Duplicate folder: ' + $_Dir + @CRLF
          $_Dup = $_Dup + 1
        Else
          $_Admin = $_Admin + $_Dir + ';'
        EndIf
        $_Af = 1
      EndIf
    Next
 
    If Not $_Af
      Select
       Case $_Dir = ''						; TRAP INVALID ELEMENT 
 
       Case InStr($_NPath, $_SRoot)				; SYSTEM PATH ELEMENT 
        If InStr($_System, $_Dir + ';')
          $_aTmp[3] = $_aTmp[3] + 'Duplicate folder: ' + $_Dir + @CRLF
          $_Dup = $_Dup + 1
        Else
          $_System = $_System + $_Dir + ';'
        EndIf
 
       Case InStr($_NPath, 'jre') Or InStr($_NPath, 'j2re')	; JAVA PATH ELEMENT 
        If InStr($_Java, $_Dir + ';')
          $_aTmp[3] = $_aTmp[3] + 'Duplicate folder: ' + $_Dir + @CRLF
          $_Dup = $_Dup + 1
        Else
          $_Java = $_Java + $_Dir + ';'
        EndIf
 
       Case InStr($_NPath, $_SDrv + '\progra') = 1		; PRIMARY APP ELEMENT 
        If InStr($_Apps1, $_Dir + ';')
          $_aTmp[3] = $_aTmp[3] + 'Duplicate folder: ' + $_Dir + @CRLF
          $_Dup = $_Dup + 1
        Else
          $_Apps1 = $_Apps1 + $_Dir + ';'
        EndIf
 
       Case 1							; SECONDARY APP ELEMENT 
        If InStr($_Apps2, $_Dir + ';')
          $_aTmp[3] = $_aTmp[3] + 'Duplicate folder: ' + $_Dir + @CRLF
          $_Dup = $_Dup + 1
        Else
          $_Apps2 = $_Apps2 + $_Dir + ';'
        EndIf
 
      EndSelect
 
    EndIf
 
  Next
 
  $_aTmp[0] = $_aPath
  $_aTmp[1] = $_Admin, $_System, $_Apps1, $_Apps2, $_Java
  $_aTmp[2] = $_Dup, $_Missing, $_Invalid
  $_aTmp[3] = Split($_aTmp[3], @CRLF)
  $PathInfo = $_aTmp
 
  Exit 0
 
EndFunction