Jump to content

_PathSplitByRegExp()


MrCreatoR
 Share

Recommended Posts

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

 

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

  • 1 month later...

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'

 

Link to comment
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

Link to comment
Share on other sites

  • 1 year later...
  • 10 months later...
  • 11 years later...
  • Developers
22 minutes ago, xJilo said:

I'm new here where I can find the file?

Which file? The code is posted in the original post so just save it yourself?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Can any body here teach me how to replace the text for $sPath into get clip that will run this file with like this: https://prnt.sc/uiu0kn not this: https://prnt.sc/uiu5qf

Idea is need to Shift + Right Click the file on Windows Explorer and copy Path then run this code: 

;===============================================================================
; 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[2], $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, "") ;FileName and extension

    Return $aRetArray

 EndFunc

 #include <Array.au3>

;$sPath = "C:\Users\JiLO\Documents\Script-Files"
$sPath = Clipget()
$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
 

I Hope someone with a big heart will help me for this problem.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...