;; 
;;=====================================================================================----- 
;;FUNCTION       PathSplit() 
;; 
;;ACTION         Returns a 4 element array with Server, Share, Path, and File 
;; 
;;AUTHOR         Glenn Barnas  
;; 
;;VERSION        3.0  - 2009/11/06 
;; 
;;HISTORY        1.0 - "sometime in 2001" - Initial Release 
;;               3.0  - 2009/11/06 - Enhancement for UNC paths 
;; 
;;SYNTAX         PathSplit("path") 
;; 
;;PARAMETERS     Path - Directory path to examine. 
;; 
;;REMARKS        Path can be relative (a\b\c) or rooted (\a\b\c), and either type can 
;;               optionally specify either a drive (D:) or UNC name (\\server\share). 
;;               If the parameter ends with a "\", it is assumed to be a directory. 
;;               If the parameter is ambiguous, the function will return it as a PATH 
;;               if the last element does not contain a "." and as a FILE if it does. 
;;               Element 2 will always contain a trailing '\' if any path component is 
;;               specified, but will be null if only a drive letter and/or filename is 
;;               provided. It will begin with a '\' only if the supplied path argument 
;;               is rooted. 
;; 
;;RETURNS        4-element array containing info about a file/path: 
;;               Element 0 = \\Server name (if specified) 
;;               Element 1 = \Share name OR Drive Letter (if specified) 
;;               Element 2 = \path\ 
;;               Element 3 = filename  
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    NT4, W2K, WXP 
;; 
;;EXAMPLES       ; Returns "","C:","\Temp\install\","" 
;;               PathSplit("C:\Temp\install") 
;;               ; Returns "\\server","C$","\Temp\","inst.bat" 
;;               PathSplit("\\server\c$\Temp\inst.bat") 
;;               ; Returns "","R:","","" 
;;               PathSplit("R:") 
; 
Function PathSplit($Path)
 
  Dim $Server, $Share, $Dir, $File, $WPath, $Rooted
  Dim $iX, $UBound, $IsFile, $Tag, $aTmp, $aDir
 
  ; initialize variables 
  $Server='' $Share='' $Dir='' $File='' $Tag=0
  $WPath = '' $Rooted = ''  $IsFile = 0
 
 
  ; If ambiguous path specified, try to identify it 
  If Right($Path,1) <> '\'
    If Exist($Path) 
      If GetFileAttr($Path) & 16
        $Path = $Path + '\'                     ; is a dir, so add trailing '\' 
      Else
        $IsFile = 1
      EndIf
    EndIf
  EndIf
 
  ; Break down into three classes of paths 
  ; UNC    - '\\server\share\path       (char 2 = '\') 
  ; Disk   - 'D:path...'                (char 2 = ':') 
  ; Simple - 'path...'                  (char 2 <> '\' or ':') 
  ; UNC is always Rooted, Disk and Simple may or may not be Rooted 
 
  Select
  ; UNC         Second char is '\', must be \\server\share\path 
  Case SubStr($Path,2,1) = '\'
    $Rooted = '\'                               ; Always rooted 
    ; remove the leading '\\' and Split into Server, Share, Path... 
    $aTmp = Split(SubStr($Path,3,Len($Path) - 2),'\')
    $Server = '\\' + $aTmp[0]
    $Share  = '\' + $aTmp[1]
    ; Build the Working Path from everything after the sharename 
    For $iX = 2 to UBound($aTmp) - 1
      $WPath = $WPath + $aTmp[$iX] + '\'
    Next
    ; add the last element with no trailing '\' 
    $WPath = $WPath + $aTmp[UBound($aTmp)]
    $aTmp = ''
 
  ; DISK        Second char is ':', must be Drive letter 
  Case SubStr($Path,2,1) = ':'
    $Share  = Left($Path,2)
    ; determine if Rooted, trim left 3 if rooted, left 2 if not 
    If SubStr($Path,3,1) = '\'
      $Rooted = '\'
      $WPath = Right($Path, Len($Path) - 3)
    Else
      $WPath = Right($Path, Len($Path) - 2)
    EndIf
 
  ; SIMPLE       Neither UNC or DISK 
  Case 1
    ; determine if Rooted, trim left 1 if rooted, nothing if not 
    If SubStr($Path,1,1) = '\'
      $Rooted = '\'
      $WPath = Right($Path, Len($Path) - 1)
    Else
      $WPath = $Path
    EndIf
 
  EndSelect
 
  ; at this point, Server and Share/Drive should be valid, and Rooted defined 
  ; WPath will be set to any path\file.ext value without leading or trailing '\' 
  ; WPath may be null if only a drive was specified (D:) as the Path 
 
  $UBound = -1
 
  If $WPath <> ''
    $aDir = Split($WPath,'\')
    $UBound = UBound($aDir)
 
 
    ; Default - last element is FILE unless overridden 
    $File = $aDir[$UBound]
 
  
    ; single element, no '.', assume DIRECTORY 
    If $UBound = 0
      If Not InStr($Path,'.') And Not $IsFile
        $Dir  = $aDir[0]+ '\'
        $File = ''
      EndIf
    Else
      For $iX = 0 to $UBound - 1 
        $Dir = $Dir + $aDir[$iX] + '\'
      Next
      If InStr($aDir[$UBound],'.') = 0 And $aDir[$UBound] <> '' And Not $IsFile
        $File = ''
        $Dir  = $Dir + $aDir[$UBound] + '\'
      EndIf
    EndIf
 
  EndIf
  $PathSplit = $Server, $Share, $Rooted + $Dir, $File
 
EndFunction