Jump to content

Recommended Posts

Posted (edited)

TheSaint requested sometime ago that _PathSplit be changed to extract the 'parent folder' of a path. After much discussion about the subject, I went ahead and drew up the following code.

Function:

Func _PathSplitEx($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sParentDir, ByRef $sFileName, ByRef $sExtension)
    Local Enum $eFilePath, $eDrive, $eDir, $eParentDir, $eFileName, $eExtension, $eMax
    Local $aReturn[$eMax], _
            $bAppended = True, _
            $iExtension = StringInStr($sFilePath, '.', $STR_NOCASESENSEBASIC, -1), $iSlash = 0

    $aReturn[$eFilePath] = $sFilePath
    If StringInStr($sFilePath, '/', $STR_NOCASESENSEBASIC) Then
        $sFilePath = StringReplace($sFilePath, '/', '\') ; Replace '/' with '\'
    EndIf
    $aReturn[$eDir] = $sFilePath
    $aReturn[$eDrive] = StringLeft($sFilePath, 2) ; Drive.
    If $aReturn[$eDrive] == '\\' Then ; UNC path.
        $iSlash = StringInStr($sFilePath, '\', $STR_NOCASESENSEBASIC, 3)
        If $iSlash Then
            $aReturn[$eDrive] = StringLeft($sFilePath, $iSlash - 1)
        EndIf
        $aReturn[$eDir] = 'A:' & StringTrimLeft($aReturn[$eDir], StringLen($aReturn[$eDrive]))
        $iExtension = StringInStr($aReturn[$eDir], '.', $STR_NOCASESENSEBASIC, -1)
    EndIf

    $iSlash = StringInStr($aReturn[$eDir], '\', $STR_NOCASESENSEBASIC, -1)
    If $iExtension Then ; If an extension exists.
        $aReturn[$eExtension] = StringTrimLeft($aReturn[$eDir], $iExtension - 1) ; Extension.
        $aReturn[$eFileName] = StringTrimRight(StringTrimLeft($aReturn[$eDir], $iSlash), StringLen($aReturn[$eExtension])) ; Filename.
    Else
        $bAppended = (StringRight($aReturn[$eDir], 1) == '\') ; Check if a backslash is appended to the end.
        If Not $bAppended Then ; If backslash doesn't exist (when it's a directory) then append to the end.
            $aReturn[$eDir] &= '\'
            $iSlash = StringInStr($aReturn[$eDir], '\', $STR_NOCASESENSEBASIC, -1)
        EndIf
    EndIf
    $aReturn[$eDir] = StringTrimLeft(StringLeft($aReturn[$eDir], $iSlash), 2) ; Path.
    $aReturn[$eParentDir] = StringTrimLeft($aReturn[$eDir], StringInStr($aReturn[$eDir], '\', $STR_NOCASESENSEBASIC, -2)) ; Parent folder.
    $aReturn[$eDir] = StringTrimRight($aReturn[$eDir], StringLen($aReturn[$eParentDir])) ; Remove parent folder from the path.
    If Not $bAppended Then
        $aReturn[$eParentDir] = StringTrimRight($aReturn[$eParentDir], 1)
    EndIf
    If $aReturn[$eDir] == '\' Then ; If no folder is present then copy the contents of the parent folder.
        $aReturn[$eDir] &= $aReturn[$eParentDir]
        $aReturn[$eParentDir] = ''
    EndIf
    $sDrive = $aReturn[$eDrive]
    $sDir = $aReturn[$eDir]
    $sParentDir = $aReturn[$eParentDir]
    $sFileName = $aReturn[$eFileName]
    $sExtension = $aReturn[$eExtension]
    Return $aReturn
EndFunc   ;==>_PathSplitEx
Example use of Function:

#include <File.au3>

Example()

#cs
    [0] C:\Program Files\Chat Tools\Skype\SkypeGUI.exe - Filepath
    [1] C: - Drive.
    [2] \Program Files\Chat Tools\ - Folder.
    [3] Skype\ - Parent folder.
    [4] SkypeGUI - Filename.
    [5] .exe - Extension.
#ce
Func Example()
    Local $sDrive = '', $sDir = '', $sParentDir = '', $sFileName = '', $sExtension = ''

    ConsoleWrite('_PathSplitEx:' & @CRLF)
    Local $aPathSplit = _PathSplitEx('C:\Program Files\Chat Tools\Skype\SkypeGUI.exe', $sDrive, $sDir, $sParentDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)

    ConsoleWrite('_PathSplitEx:' & @CRLF)
    $aPathSplit = _PathSplitEx('C:\user\docs\Letter.txt', $sDrive, $sDir, $sParentDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)

    ConsoleWrite('_PathSplit:' & @CRLF)
    $aPathSplit = _PathSplit('C:\user\docs\Letter.txt', $sDrive, $sDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)

    ConsoleWrite('_PathSplitEx:' & @CRLF)
    $aPathSplit = _PathSplitEx('C:\user\docs\', $sDrive, $sDir, $sParentDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)

    ConsoleWrite('_PathSplitEx:' & @CRLF)
    $aPathSplit = _PathSplitEx('C:\user\docs', $sDrive, $sDir, $sParentDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)

    ConsoleWrite('_PathSplitEx:' & @CRLF)
    $aPathSplit = _PathSplitEx('\\Server01\user\docs\Letter.txt', $sDrive, $sDir, $sParentDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)

    ConsoleWrite('_PathSplit:' & @CRLF)
    $aPathSplit = _PathSplit('\\Server01\user\docs\Letter.txt', $sDrive, $sDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)

    ConsoleWrite('_PathSplitEx:' & @CRLF)
    $aPathSplit = _PathSplitEx('\\Server01/user\docs/Letter.txt', $sDrive, $sDir, $sParentDir, $sFileName, $sExtension)
    Print1DArray($aPathSplit)
EndFunc   ;==>Example

Func Print1DArray(ByRef Const $aArray, $iStart = Default, $iEnd = Default, $sDelim = Default)
    If $iEnd = Default Then $iEnd = UBound($aArray) - 1
    If $iStart = Default Then $iStart = 0
    If $sDelim = Default Then $sDelim = @CRLF
    Local $iIndex = 0
    For $i = $iStart To $iEnd
        ConsoleWrite('[' & $iIndex & '] ' & $aArray[$i] & @CRLF)
        $iIndex += 1
    Next
    ConsoleWrite(@CRLF)
EndFunc   ;==>Print1DArray
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

I would be very grateful if people could test it and pick faults with the code. Thanks.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Hi guinness, sorry for my english, Error using the

$Path = 'C:\Program Files\Chat Tools\Skype\SkypeGUI' ;example SkypeGUI is not folder, but files with no extension
;or
$Path = '\\?\C:\Program Files\Chat Tools\Skype\SkypeGUI.exe'

there a problem with (non) extension, I know, because weeks ago a write a function like this

;'C:\Program Files\Chat Tools\Skype\SkypeGUI.exe'
;'C:\Program Files\Chat Tools\Skype\SkypeGUI - (1).exe'
;'C:\Program Files\Chat Tools\Skype\SkypeGUI - (2).exe'
;'C:\Program Files\Chat Tools\Skype\SkypeGUI - (3).exe'
;
;or - example SkypeGUI is not folder, but files with no extension
;'C:\Program Files\Chat Tools\Skype\SkypeGUI'
;'C:\Program Files\Chat Tools\Skype\SkypeGUI - (1)'
;'C:\Program Files\Chat Tools\Skype\SkypeGUI - (2)'
;'C:\Program Files\Chat Tools\Skype\SkypeGUI - (3)'
Func _FileExistsEx(ByRef $sFilePath, $iFileExists = 0)
    While FileExists($sFilePath)
        $iFileExists += 1
        $sFilePath = StringRegExpReplace($sFilePath & " ", "( - \(\d+\))?(\.[^\.\\]*)?(\h)$", " - (" & $iFileExists & ")$2")
    WEnd
EndFunc

here try this is more simple (looks perfect thanks to RegExp ehhh) and is almost 30% faster

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Posted (edited)

  On 1/22/2013 at 3:57 PM, 'guinness said:

I would be very grateful if people could test it and pick faults with the code. Thanks.

There is still a message box in the function, and it's using a magic number. Posted Image Edited by Mat
Posted

  On 1/23/2013 at 12:11 AM, 'Mat said:

There is still a message box in the function, and it's using a magic number. Posted Image

Just a simple debug MsgBox that was left over, now removed.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/22/2013 at 10:00 PM, 'DXRW4E said:

there a problem with (non) extension, I know, because weeks ago a write a function like this

Of course it does. This isn't a bug as you very well know.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

There are some characters that are not allowed in Windows, which can not be assigned to folders and files. Your function does not exclude them.

For example: C:Program FilesChat ToolsSkypeSkypeGUI?.exe

[0] C:Program FilesChat ToolsSkypeSkypeGUI?.exe

[1] C:

[2] Program FilesChat Tools

[3] Skype

[4] SkypeGUI?

[5] .exe

Posted

Neither does _PathSplit in File.au3, which this is "loosely" based on. Both functions don't check whether a path is valid, this is down to the developer to do, not the function.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/23/2013 at 1:20 PM, 'kiritoproject said:

I see. But a suggestion would be to allow, if you want, check the path. Just because it also lacks official UDF.

It's something that could be considered.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 3 weeks later...
Posted

Doh! Finally noticed this. Excellent!

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

  Reveal hidden contents

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Posted

Thanks for liking it.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 year later...
Posted

I updated the function to be a little more readable for the future.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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