Recently Browsing 0 members
No registered users viewing this page.
Recently I had the need to split paths like _PathSplit outside of AutoIt.
Regular expressions seemed to be the way to go and it was fun to learn a bit more about their power.
I backported it to AutoIt and now use it instead of _PathSplit.
This should match any combination of:
servershare[$] | Drive path filename .extention Some caveats:
It only returns an array (returning strings via byref aswell seemed a tad overkill / messy)
The Drive letter retains its trailing "" NOT the path (this seemed more sane as the path should be relative not anchored to the root of the drive) - Found that this was not so good for things like "C:" as the path shoud be set to "" not nothing
; #FUNCTION# ==================================================================================================================== ; Name...........: _PathSplitRegEx ; Description ...: Splits a path into the drive, directory, file name and file extension parts. An empty string is set if a part is missing. ; Syntax.........: _PathSplitRegEx($sPath) ; Parameters ....: $sPath - The path to be split (Can contain a UNC server or drive letter) ; Return values .: Success - Returns an array with 5 elements where 0 = original path, 1 = drive, 2 = directory, 3 = filename, 4 = extension ; Author ........: Gibbo ; Modified.......: ; Remarks .......: This function does not take a command line string. It works on paths, not paths with arguments. ; This differs from _PathSplit in that the drive letter or servershare retains the "" not the path ; RegEx Built using examples from "Regular Expressions Cookbook (O’Reilly Media, 2009)" ; Related .......: _PathSplit, _PathFull, _PathMake ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _PathSplitRegEx($sPath) if $sPath="" then Return SetError(1,0,"") Local Const $rPathSplit = '^(?i)([a-z]:|[a-z0-9_.$]+[a-z0-9_.]+$?)?((?:[^/:*?"<>|rn]+)*)?([^/:*?"<>|rn.]*)((?:.[^./:*?"<>|rn]+)?)$' Local $aResult = StringRegExp($sPath, $rPathSplit, 2) Switch @error Case 0 Return $aResult Case 1 Return SetError(2, 0, "") Case 2 ;This should never happen! EndSwitch EndFunc ;==>_PathSplitRegEx Anyhow, I hope someone else finds this useful
Edit: @ScriptFullPath replaced with the parameter $sPath - Thanks czardas. Should have reread before posting.
Edit2: Changed regex to move the trailing "" from the "root" to the begining of the path.
Edit3: Edit destroyed the regex (why??) added below as well in plain code tags just in case it happens again:
Local Const $rPathSplit = '^(?i)([a-z]:|[a-z0-9_.$]+[a-z0-9_.]+$?)?((?:[^/:*?"<>|rn]+)*)?([^/:*?"<>|rn.]*)((?:.[^./:*?"<>|rn]+)?)$'