Jump to content



Photo

_PathSplitByRegExp()


  • Please log in to reply
5 replies to this topic

#1 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,241 posts

Posted 01 March 2007 - 02:38 AM

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

AutoIt         
;=============================================================================== ; 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, 30 January 2011 - 08:56 PM.

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

Posted Image AutoIt Russian CommunityPosted Image Projects: 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 ProgramPosted Image UDFs: 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 UDFPosted Image Examples: 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 DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team






#2 taz742

taz742

    Seeker

  • Active Members
  • 46 posts

Posted 01 March 2007 - 04:49 AM

Great Job!!! :whistle:
Thanks for sharing It :)
Posted Image

#3 sshrum

sshrum

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 538 posts

Posted 10 April 2007 - 07:56 PM

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 :: Shrum Consultingsean[at]shrum[dot]net :: http://www.shrum.net---------------Skinner: 'Oh, brandishing your buttocks is only making me angrier!'Me: 'Make it idiot-proof, and someone will make a better idiot'Me (sort of): 'Give a man a fish and you feed him for a day. Teach a man to fish and he'll snag some part of his body with a rusty hook, get tetanus, and surely die'---------------All my published AU3-based apps and utilities

#4 eltorro

eltorro

    more or less the same as the latter of the former.

  • Active Members
  • PipPipPipPipPipPip
  • 596 posts

Posted 11 April 2007 - 03:26 AM

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:
Plain Text         
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
  • Loz likes this

#5 Cdma1X

Cdma1X

    Wayfarer

  • Banned (NOT IN USE)
  • 92 posts

Posted 07 October 2008 - 12:20 PM

very good script+Example :P

#6 slayerz

slayerz

    Prodigy

  • Active Members
  • PipPipPip
  • 160 posts

Posted 03 September 2009 - 08:56 AM

Really interested in Regular Expression, but I really dun understand the example in the help file :D
AUTOIT I'm lovin' it!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users