Jump to content

_PathSplitRegEx


Gibbo
 Share

Recommended Posts

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 :D

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]+)?)$'
Edited by Gibbo
Link to comment
Share on other sites

There appears to be an editing msitake in this function. @ScriptFullPath needs to be replaced with the parameter $sPath , it's the sort of thing I might overlook myself during testing. Not examined it in detail, but it seems to be working.

Edited by czardas
Link to comment
Share on other sites

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

@czardas - Thanks for picking that up :D

@MrCreatoR

I took a look and tested:

#include <File.au3>
#include <Array.au3>
$aSplit = _PathSplitByRegExp("serversharepathfile.ext")
_ArrayDisplay($aSplit)

$aSplit = _PathSplitRegEx("serversharepathfile.ext")
_ArrayDisplay($aSplit)

Local $szDrive, $szDir, $szFName, $szExt
Local $TestPath = _PathSplit("serversharepathfile.ext", $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay($TestPath, "Demo _PathSplit()")

Yours seems to fail on UNC paths

[0]|serversharepathfile.ext
[1]|
[2]|serversharepath
[3]|serversharepathfile
[4]|serversharepathfile.ext
[5]|serversharepath
[6]|file.ext
[7]|file
[8]|ext

Also returning all those combinations seems a bit of overkill considering any of those could be reconstructed from just "root,folder,file,ext" :huh:

[0]|serversharepathfile.ext
[1]|servershare
[2]|path
[3]|file
[4]|.ext

Mine groups the share with the server name as this is the "root" of any folder structure in a share.

[0]|serversharepathfile.ext
[1]|server
[2]|sharepath
[3]|file
[4]|.ext

_PathSplit does a good job but groups the share name with the path and I didn't much care for all the "ByRef" strings and such when $aSplit[0] -> $aSplit[4] already contained the values.

All up though it was more of a fun exercise to make/port. I found it useful and just thought others would too.

:)

Link to comment
Share on other sites

  • 2 weeks later...

I took a look and tested:

Your function does not return an array, and what shouldn't never happen, happened:

#include <Array.au3>

$aSplit = _PathSplitRegEx("serversharepathfile.ext")
_ArrayDisplay($aSplit)

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
            MsgBox(64, 'Title', 'This should never happen? Well, it''s happened :)')
    EndSwitch
EndFunc

:)

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • 2 weeks later...

The reason it didn't work for you is that the forums seem to have destroyed the regex

I had:

Local Const $rPathSplit = '^(?i)([a-z]:|\\\\[a-z0-9_.$]+\\[a-z0-9_.]+\$?)?(\\(?:[^\\/:*?"<>|\r\n]+\\)*)?([^\\/:*?"<>|\r\n.]*)((?:\.[^.\\/:*?"<>|\r\n]+)?)$'

You used:

Local Const $rPathSplit = '^(?i)([a-z]:|[a-z0-9_.$]+[a-z0-9_.]+$?)?((?:[^/:*?"<>|rn]+)*)?([^/:*?"<>|rn.]*)((?:.[^./:*?"<>|rn]+)?)$'
Link to comment
Share on other sites

So often I find that I only need part of the filename, ie, just the ext, or just the Drive, so I created these fairly simple functions.

Func File_GetDrive($sFilespec)
    Return StringRegExpReplace($sFilespec, '^([A-Za-z]:)?(?:.*)$', '1')
EndFunc
Func File_GetPath($sFilespec)
    If Not StringRegExp($sFilespec, "", 0) Then Return ''
    Return StringRegExpReplace($sFilespec, '^([A-Za-z]:|[A-Za-z]:||)(.*)?(?:[^]+)?$', '2')
EndFunc
Func File_GetName($sFilespec)
    If StringRegExp($sFilespec, "$", 0) Then Return ''
    Return StringRegExpReplace($sFilespec, '^(?:[A-Za-z]:|)(?:.*|)(.*?)(?:.[^.]*|)$', '1')
EndFunc
Func File_GetExt($sFilespec)
    If StringRegExp($sFilespec, "$", 0) Or Not StringRegExp($sFilespec, ".", 0) Then Return ''
    Return StringRegExpReplace($sFilespec, '^(?:[A-Za-z]:|)(?:.*|)(?:.*?)(.[^.]*)?$', '1')
EndFunc
Func File_GetDriveAndPath($sFilespec)
    Return StringRegExpReplace($sFilespec, '^([A-Za-z]:|)(?:|)(.*|)(?:[^]*)', '12')
EndFunc
Func File_GetNameAndExt($sFilespec)
    Return StringRegExpReplace($sFilespec, '^(?:[A-Za-z]:|.*|)([^.].*)?(.[^.]*)?$', '1')
EndFunc
Func File_GetParent($sFilespec)
    Local $vParts = StringRegExp($sFilespec,"(.*)([^]+?)$",3)
    If Not @error Then Return $vParts[0]
EndFunc
Func Win_FilenameSafe($sFilespec)
    Return StringRegExpReplace($sFilespec,"x00|x01|x02|x03|x04|x05|x06|x07|x08|x09|x0A|x0B|x0C|x0D|x0E|x0F|x10|x11|x12|x13|x14|x15|x16|x17|x18|x19|x1Ax1B|x1C|x1D|x1E|x1F|x22|x2A|x2F|x3A|x3C|x3E|x3F|x5C|x7C|x81|x8D|x8F|x90|x9D|xAD","")
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...