EDIT1: I have decided to change the function, because i has thought that all elements should be returned (see comments)...
EDIT2: Added support for path that have backslash (exmp: C:/my dir/test.zip).
EDIT3: Added Full path without drive letter and without FileName and extension
;=============================================================================== ; Function Name: _PathSplitByRegExp() ; Description: Splits the path to 9 elements. ; Parameter(s): $sPath - Path to split. ; Requirement(s): ; Return Value(s): On seccess - Array $aRetArray that contain 9 elements: ; $aRetArray[0] = Full path ($sPath) ; $aRetArray[1] = Drive letter ; $aRetArray[2] = Path without FileName and extension ; $aRetArray[3] = Full path without File Extension ; $aRetArray[4] = Full path without drive letter ; $aRetArray[5] = Full path without drive letter and without FileName and extension ; $aRetArray[6] = FileName and extension ; $aRetArray[7] = Just Filename ; $aRetArray[8] = Just Extension of a file ; ; On failure - If $sPath is not a valid path (the path is not splitable), then original $sPath is returned. ; If $sPath does not include supported delimiters or it's emty, @error set to 1 and -1 is returned. ; ; Note(s): The path can include backslash as well (exmp: C:/test/test.zip). ; ; Author(s): G.Sandler a.k.a CreatoR (MrCreatoR) - Thanks to amel27 for help with RegExp ;=============================================================================== Func _PathSplitByRegExp($sPath) If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then Return SetError(1, 0, -1) EndIf Local $aRetArray[9], $pDelim = "" If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\" EndIf If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then $pDelim = "//" EndIf If $pDelim = "" Then $pDelim = "/" EndIf If Not StringInStr($sPath, $pDelim) Then Return $sPath EndIf If $pDelim = "\" Then $pDelim &= "\" EndIf $aRetArray[0] = $sPath ;Full path $aRetArray[1] = StringRegExpReplace($sPath, $pDelim & '.*', $pDelim) ;Drive letter $aRetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '') ;Path without FileName and extension $aRetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '') ;Full path without File Extension $aRetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & ')', '') ;Full path without drive letter $aRetArray[5] = StringRegExpReplace($aRetArray[4], $pDelim & '[^' & $pDelim & ']*$', '') ;Full path without drive letter and without FileName and extension $aRetArray[6] = StringRegExpReplace($sPath, '^.*' & $pDelim, '') ;FileName and extension $aRetArray[7] = StringRegExpReplace($sPath, '.*' & $pDelim & '|\.[^.]*$', '') ;Just Filename $aRetArray[8] = StringRegExpReplace($aRetArray[6], '^.*\.|^.*$', '') ;Just Extension of a file Return $aRetArray EndFunc
Example:
#include <Array.au3> $sPath = "C:\my test\path\file.zip" $aPathArr = _PathSplitByRegExp($sPath) If IsArray($aPathArr) Then _ArrayDisplay($aPathArr, "Demo of _PathSplitRegExp()") ElseIf $aPathArr = $sPath Then MsgBox(64, "Demo of _PathSplitRegExp()", $aPathArr) Else MsgBox(48, "Error", "The path is not correct") EndIf
If there is no error, we get this displayed (from example #1):
Thanks to SmOke_N for a direction about the returned values and shorter way to arrange this func[0]= C:\my test\path\file.zip
[1] = C:\
[2] = C:\my test\path
[3] = C:\my test\path\file
[4] = my test\path\file.zip
[5] = my test\path
[6] = file.zip
[7] = file
[8] = zip
Edited by MrCreatoR, 30 January 2011 - 08:56 PM.









