Jump to content

Recommended Posts

Posted (edited)

Because I'm bored using the UDF's _PathSplit() function & its returned array: I write this function.

Hope this help someone.

New script using RegExp: _FileGetPathByRegExp()

Based on _PathSplitByRegExp() by G.Sandler a.k.a CreatoR (MsCreatoR) so thanks to him :whistle:

$source = @ProgramFilesDir & "\AutoIt3\Include\File.au3"
MsgBox("", "_FileGetPathByRegExp() - example", @TAB & "$source = " & @TAB & @TAB & $source & @CRLF & @CRLF & _
        "_FileGetPathByRegExp($source, 1):" & @TAB & _FileGetPathByRegExp($source, 1) & @CRLF & _
        "_FileGetPathByRegExp($source, 12):" & @TAB & _FileGetPathByRegExp($source, 12) & @CRLF & _
        "_FileGetPathByRegExp($source, 123):" & @TAB & _FileGetPathByRegExp($source, 123) & @CRLF & _
        "_FileGetPathByRegExp($source, 2):" & @TAB & _FileGetPathByRegExp($source, 2) & @CRLF & _
        "_FileGetPathByRegExp($source, 23):" & @TAB & _FileGetPathByRegExp($source, 23) & @CRLF & _
        "_FileGetPathByRegExp($source, 234):" & @TAB & _FileGetPathByRegExp($source, 234) & @CRLF & _
        "_FileGetPathByRegExp($source, 3):" & @TAB & _FileGetPathByRegExp($source, 3) & @CRLF & _
        "_FileGetPathByRegExp($source, 34):" & @TAB & _FileGetPathByRegExp($source, 34) & @CRLF & _
        "_FileGetPathByRegExp($source, 4):" & @TAB & _FileGetPathByRegExp($source, 4))

;===============================================================================
; Function Name:    _FileGetPathByRegExp($sPath [,$opt])
; Description:      Parse a fullpath to output what choosen in $sOutput parameter.
;                    (Fullpath can be a UNC server)
; Requirement(s):   AutoIt 3.2.2.0.
; Parameter(s):        $opt
;                                "1"   =>  Drive ("Drive:")
;                    (Default)   "12"  =>  DirFullPath ("Drive:\Path\")
;                                "123" =>  DirFullPath & Filename ("Drive:\Path\File")
;                                "2"   =>  Path ("\Path\")
;                                "23"  =>  Path & Filename ("\Path\File")
;                                "234" =>  Path & FullFileName  ("\Path\File.Ext")
;                                "3"   =>  Filename ("File")
;                                "34"  =>  FullFileName ("File.Ext")
;                                "4"   =>  Extention (".Ext")
; Return Value(s):  String
; Author(s):        Taz742 based on _PathSplitByRegExp() by G.Sandler a.k.a CreatoR (MsCreatoR)
;===============================================================================
Func _FileGetPathByRegExp($sPath, $opt = 12)
    Local $pDelim = "", $oPath
    If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\\"
    If $pDelim = "" Then Return SetError(1, 0, $sPath)

    If StringMid($sPath, 1, 2) = "\\" Then
        $sPath = StringMid($sPath, 3)
        Switch $opt
            Case 1
                $oPath = StringRegExpReplace($sPath, $pDelim & '.*', $pDelim)
            Case 12
                $oPath = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '') & "\"
            Case 123
                $oPath = StringRegExpReplace($sPath, '\.[^.]*$', '')
            Case 2
                $oPath = StringMid(StringRegExpReplace(StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & ')', ''), $pDelim & '[^' & $pDelim & ']*$', '') & "\", StringLen(StringRegExpReplace($sPath, $pDelim & '.*', $pDelim)) + 1)
            Case 23
                $oPath = StringMid(StringRegExpReplace(StringRegExpReplace($sPath, '\.[^.]*$', ''), '(?i)([A-Z]:' & $pDelim & ')', ''), StringLen(StringRegExpReplace($sPath, $pDelim & '.*', $pDelim)) + 1)
            Case 234
                $oPath = StringMid(StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & ')', ''), StringLen(StringRegExpReplace($sPath, $pDelim & '.*', $pDelim)) + 1)
            Case 3
                $oPath = StringRegExpReplace(StringRegExpReplace($sPath, '^.*' & $pDelim, ''), '\.[^.]*$', '')
            Case 34
                $oPath = StringRegExpReplace($sPath, '^.*' & $pDelim, '')
            Case 4
                $oPath = "." & StringRegExpReplace($sPath, '^.*\.', '')
        EndSwitch
        If $opt = 1 Or $opt = 12 Or $opt = 123 Then $oPath = "\\" & $oPath
    Else
        Switch $opt
            Case 1
                $oPath = StringRegExpReplace($sPath, $pDelim & '.*', $pDelim)
            Case 12
                $oPath = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '') & "\"
            Case 123
                $oPath = StringRegExpReplace($sPath, '\.[^.]*$', '')
            Case 2
                $oPath = StringRegExpReplace(StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & ')', ''), $pDelim & '[^' & $pDelim & ']*$', '') & "\"
            Case 23
                $oPath = StringRegExpReplace(StringRegExpReplace($sPath, '\.[^.]*$', ''), '(?i)([A-Z]:' & $pDelim & ')', '')
            Case 234
                $oPath = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & ')', '')
            Case 3
                $oPath = StringRegExpReplace(StringRegExpReplace($sPath, '^.*' & $pDelim, ''), '\.[^.]*$', '')
            Case 34
                $oPath = StringRegExpReplace($sPath, '^.*' & $pDelim, '')
            Case 4
                $oPath = "." & StringRegExpReplace($sPath, '^.*\.', '')
        EndSwitch
    EndIf
    Return $oPath
EndFunc   ;==>_FileGetPathByRegExp
Edited by taz742
Posted
Here is my version using StringRegExp to split the path (_PathSplitByRegExp()) (I think fastest and much shorter :whistle: )

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • 3 weeks later...
Posted

Why re-invent the entire wheel? Why not leverage existing functionality by using *proven* code instead of trying to roll your own? Use _PathSplit() to split the file into it's constituent parts and then you can use your overly-complex flag system to re-build the relevant parts. In fact, your code can probably be re-written with this clever little piece:

#include <File.au3>

Func _FileGetPath($FileFullPath, $sOutput = 12)
    Local $sNull
    Local $aPath = _PathSplit($FileFullPath, $sNull, $sNull, $sNull, $sNull)
    Local $aIndices = StringSplit($sOutput, "")
    Local $aOutput[4]
    For $i = 1 To UBound($aIndices) - 1
        Local $index = $aIndices[$i]
        If $index > 0 And $index < 5 Then $aOutput[$index - 1] = $aPath[$index]
    Next
    Return _PathMake($aOutput[0], $aOutput[1], $aOutput[2], $aOutput[3])
EndFunc ; _FileGetPath()

If that doesn't do exactly what the code in the first post does, it should only require very minor tweaking to get there (It is, however, completely untested).

Posted

I've rewritten _PathSplit() to be much faster. If it's still not fast enough for you, then quite honestly, I think you have your priorities in the wrong place. Splitting paths isn't as trivial as one might think. I'd rather use a pretty fast but highly reliable back-end than I would some super fast but untested code. Additionally, none of the functions posted here support paths separated with / which are valid in Windows (_PathSplit() does support that). So as of right now, none of the code posted is actually as feature rich as _PathSplit() so any speed comparisons are moot.

Posted

I have edit my post here a litle, so now the function support pathes that include backslash (/) as separater :whistle: .

P.S

I made this function only for personal convinient, because every time pass the vars that must be declared, is not very convinient somtimes (for me) - and also because i wanted to practic with RegExp a litle :P .

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

  MsCreatoR said:

I have edit my post here a litle, so now the function support pathes that include backslash (/) as separater :whistle: .

P.S

I made this function only for personal convinient, because every time pass the vars that must be declared, is not very convinient somtimes (for me) - and also because i wanted to practic with RegExp a litle :P .

Convenience isn't an excuse to re-invent the wheel when all you have to do is put a tire around the existing wheel to make a superior product. I am completely at a loss why people bitch about the syntax of _PathSplit() so they write their own version instead of using _PathSplit() but hiding the inconvenient syntax behind an easier to use function. It's like everybody thinks, "This function doesn't work how I want so I must completely re-write it". Nobody ever thinks, "This function doesn't work how I want so maybe I'll just wrap a little bit of code around it to make it work like I want". Quite frankly, I think it's stupid how everybody approaches _PathSplit() and it's very frustrating as well. I've spent a lot of time writing and tweaking that function and nobody uses it "because it's hard to use" yet tons of people need the functionality it provides. That function has existed longer than most of you have used AutoIt and it's very annoying to have such an old, well tested, virtually bullet-proof function discarded simply because nobody is smart enough to figure out how to use it to write more specialized functions using it as a base. I also find it slightly disturbing that nobody actually makes the logical leap to wrap things when the original doesn't quite interface how you like.

Posted

I do not claim to believe that I did better than "you and all developers joined together" by posting this function on the forum. Far from me this idea I just wanted to share my experiment in the case of it would be useful to somebody. PathSplit() done good work that's just me and arrays aren't friends. I like functions which turns over me directly the anticipated result. So I'm sorry to have annoyed or upset you. I thought that this forum was made for that: to share and why not if it's possible, to improve this superb tool which is AutoIt. Sincerely.

Posted

  Quote

Nobody ever thinks, "This function doesn't work how I want so maybe I'll just wrap a little bit of code around it to make it work like I want"

I was thinking about it, but somtimes it's easy to write a new one, then change the code that not writen by you.

  Quote

it's very frustrating as well

I understand you, i know what you mean (from other things), and my advice about it, is not to put attention about those things, i am not trying to “steal” the place of your function, i am just wanted to show that i was learning somthing (in RegExp in this case), because i have no one to show him what i do in AutoIT :whistle:, and this is somtimes can be frustrating to.

And always if someone ask here on the forum, about the functionality that do _PathSplit, i always will direct him to the _PathSplit() function - because it's like you say:

  Quote

such an old, well tested, virtually bullet-proof function

:P

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

  Quote

nobody is smart enough to figure out how to use it to write more specialized functions using it as a base

I don't understand the problem here; it is a good function, and nobody uses the bad syntax;

So why doesn't the original writer or one of the current devs simply wrap the good function so the standard UDF gets changed [or new function to keep back-compatability] to have good syntax, and it gets used a lot? - win/ win for everyone?

Best, Randall

Edited by randallc
Posted

  randallc said:

So why doesn't the original writer or one of the current devs simply wrap the good function so the standard UDF gets changed [or new function to keep back-compatability] to have good syntax, and it gets used a lot? - win/ win for everyone?

Best, Randall

Yes this could be a good solution.
Posted

  MsCreatoR said:

I was thinking about it, but somtimes it's easy to write a new one, then change the code that not writen by you.

Even after having it explained to you, you still don't get it? You don't have to re-write anything. You don't have to do anything with _PathSplit() except use it - once.

Func _FileGetPath($sFile)
    Local $sDir, $sPath, $NULL
    _PathSplit($sFile, $sDir, $sPath, $NULL, $NULL)
    Return _PathMake($sDir, $sPath, "", "")
EndFunc

DONE. No arrays. No rewriting anything. No complex flag logic. It should be 5 lines of code and 30 seconds worth of work to hide away the ugly interface. Is this really so hard to figure out?

I find it very disheartening that nobody can figure this out. This is like the most basic example of code re-use imaginable, yet nobody ever seems to get it on their own.

Posted

Valik

  Quote

Even after having it explained to you, you still don't get it?

Sorry, i do get it, it's just to hard for me to explain on english, i was learning it from the TV :whistle:.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • 3 weeks later...
Posted (edited)

Hi,

Here it is clearer, I think?; [* and shows there is a bug in obtaining the drive name from UNC path; it only gives Computer name?]

Best, Randall

;pathsplit.au3
#include <File.au3>
;~ local $sPath=@ScriptDir&"\hi.txt"
Local $sPath = "\\Dell4700\C_drive\winword\excel\hi.txt"
MsgBox(0, "", "_FileGetDriveName($sPath,1)=" & _FileGetDriveName($sPath,1)); set $UNC=1 if UNC directory needs 2 parts and path therefore strips 1 
MsgBox(0, "", "_FileGetPath($sPath,1)=" & _FileGetPath($sPath,1)); set $UNC=1 if UNC directory needs 2 parts and path therefore strips 1 
MsgBox(0, "", "_FileGetDriveName($sPath)=" & _FileGetDriveName($sPath))
MsgBox(0, "", "_FileGetPath($sPath)=" & _FileGetPath($sPath))
MsgBox(0, "", "_FileGetFileName($sPath)=" & _FileGetFileName($sPath))
MsgBox(0, "", "_FileGetExt($sPath)=" & _FileGetExt($sPath))
Func _FileGetExt($sFile1)
    Local $NULL, $sExt
    _PathSplit($sFile1, $NULL, $NULL, $NULL, $sExt)
    Return StringTrimLeft($sExt, 1)
EndFunc   ;==>_FileGetExt
Func _FileGetPath($sFile1,$UNC=0)
    Local $sDr, $sPath, $NULL
    _PathSplit($sFile1, $sDr, $sPath, $NULL, $NULL)
    If StringLeft($sDr, 2) = "\\" And StringInStr($sPath, "\", 0,2)  and $UNC Then
        $i_pos = StringInStr($sPath, "\", 0, 2)
        $sPath = StringMid($sPath, $i_pos )
    EndIf
    Return $sPath
EndFunc   ;==>_FileGetPath
Func _FileGetDriveName($sFile1,$UNC=0)
    Local $sDr, $sPath, $NULL
    _PathSplit($sFile1, $sDr, $sPath, $NULL, $NULL)
    If StringLeft($sDr, 2) = "\\" And StringInStr($sPath, "\",0, 2) and $UNC Then
        $i_pos = StringInStr($sPath, "\", 0, 2)
        $sDr &= StringLeft($sPath, $i_pos - 1)
    EndIf
    Return $sDr
EndFunc   ;==>_FileGetDriveName
Func _FileGetFileName($sFile1)
    Local $NULL, $NULL, $sFile1Name, $sExt
    _PathSplit($sFile1, $NULL, $NULL, $sFile1Name, $sExt)
    Return $sFile1Name & $sExt
EndFunc   ;==>_FileGetFileName
[EDIT] - fixed?

[EDIT] - fixed2?

Edited by randallc

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
  • Recently Browsing   0 members

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