Sign in to follow this  
Followers 0
MrCreatoR

_PathSplitByRegExp()

6 posts in this topic

#1 ·  Posted (edited)

Here is function that return an array with splited path:

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

[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

Thanks to SmOke_N for a direction about the returned values and shorter way to arrange this func :whistle:. Edited by MrCreatoR

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...

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

Share this post


Link to post
Share on other sites



Great Job!!! :whistle:

Thanks for sharing It :)

Share this post


Link to post
Share on other sites

I have to split filenames and extensions from full paths alot. I like the idea but your function doesn't handle UNC-based filenames (blows it on array[1]):

$Path = "\\nas\public\videos\Hackers.avi" = '\' instead of '\\nas'


Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Share this post


Link to post
Share on other sites

Try this

Func _PathSplitByRegExp($sPath)
    If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then Return SetError(1, 0, -1)
    Local $RetArray[8], $pDelim = ""
    If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\"
    If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then $pDelim = "//"
    If $pDelim = "" Then $pDelim = "/"
    If Not StringInStr($sPath, $pDelim) Then Return $sPath
    If $pDelim = "\" Then $pDelim &= "\"
    $RetArray[0] = $sPath
     $RetArray[1] = StringReplace($sPath,StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*|\\w*\\)', ''),"")
    $RetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '')
    $RetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '')
     $RetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*\\|\\w*\\)', '')
    $RetArray[5] = StringRegExpReplace($sPath, '^.*' & $pDelim, '')
    $RetArray[6] = StringRegExpReplace($RetArray[5], '\.[^.]*$', '')
    $RetArray[7] = StringRegExpReplace($sPath, '^.*\.', '')
    Return $RetArray
EndFunc

Results:

C:/my test/path/file.zip
[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] =file.zip
[6] =file
[7] =zip

\\nas\public\videos\Hackers.avi
[0] =\\nas\public\videos\Hackers.avi
[1] =\\nas
[2] =\\nas\public\videos
[3] =\\nas\public\videos\Hackers
[4] =public\videos\Hackers.avi
[5] =Hackers.avi
[6] =Hackers
[7] =avi

C:\my test\path\file.zip
[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] =file.zip
[6] =file
[7] =zip

Regards

1 person likes this

Share this post


Link to post
Share on other sites

very good script+Example :P

Share this post


Link to post
Share on other sites

Really interested in Regular Expression, but I really dun understand the example in the help file :D


AUTOIT[sup] I'm lovin' it![/sup]

Share this post


Link to post
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
Sign in to follow this  
Followers 0