Jump to content
VIP

[Help] Path Split WITH StringRegExp

Recommended Posts

VIP

Need help to make function better :) with full infomation

#include <Array.au3>
#include <File.au3>

_TEST(@ScriptFullPath)
_TEST("A:")
_TEST("A:\B.c")
_TEST("D:\E\F\")
_TEST("G:\H/../J.k/")
_TEST("M:\N\k..J.k")
_TEST("D:\E\F\..\G\G\I..J.K.M")

Func _TEST($sFilePath)
    Local $sDrive = "", $sFullPathDir = "", $sDirPath = "", $sDirName = "", $sFileName = "", $sFileNameExt = "", $sExtension = "", $sExt = ""
    Local $aPathSplit = _PathSplitByRef($sFilePath, $sDrive, $sFullPathDir, $sDirPath, $sDirName, $sFileName, $sFileNameExt, $sExtension, $sExt)

    ConsoleWrite("!Path IN     : " & $sFilePath & @CRLF)    ; C:\Windows\System32\etc\hosts.exe
    ConsoleWrite("- Driver     : " & $sDrive & @CRLF)       ; C:
    ConsoleWrite("- DirPath    : " & $sFullPathDir & @CRLF) ; C:\Windows\System32\etc\etc
    ConsoleWrite("- DirPath    : " & $sDirPath & @CRLF)     ; \Windows\System32\etc\
    ConsoleWrite("- DirName    : " & $sDirName & @CRLF)     ; etc
    ConsoleWrite("- FileName   : " & $sFileName & @CRLF)    ; hosts
    ConsoleWrite("- FileNameExt: " & $sFileNameExt & @CRLF) ; hosts.exe
    ConsoleWrite("- Extension  : " & $sExtension & @CRLF)   ; .exe
    ConsoleWrite("- Ext        : " & $sExt & @CRLF & @CRLF) ; exe

;~     ConsoleWrite("!Path IN     : " & $aPathSplit[0] & @CRLF)   ; C:\Windows\System32\etc\hosts.exe
;~     ConsoleWrite("- Driver     : " & $aPathSplit[1] & @CRLF)   ; C:
;~     ConsoleWrite("- DirPath    : " & $aPathSplit[2] & @CRLF)   ; C:\Windows\System32\etc\etc
;~     ConsoleWrite("- DirPath    : " & $aPathSplit[3] & @CRLF)   ; \Windows\System32\etc\
;~     ConsoleWrite("- DirName    : " & $aPathSplit[4] & @CRLF)   ; etc
;~     ConsoleWrite("- FileName   : " & $aPathSplit[5] & @CRLF)   ; hosts
;~     ConsoleWrite("- FileNameExt: " & $aPathSplit[6] & @CRLF)   ; hosts.exe
;~     ConsoleWrite("- Extension  : " & $aPathSplit[7] & @CRLF)   ; .exe
;~     ConsoleWrite("- Ext        : " & $aPathSplit[8] & @CRLF)   ; exe

;~  _ArrayDisplay($aPathSplit, "_PathSplit of " & $sFilePath)
EndFunc   ;==>_TEST

Func _PathSplitByRef($sFilePath, ByRef $sDrive, ByRef $sFullPathDir, ByRef $sDirPath, ByRef $sDirName, ByRef $sFileName, ByRef $sFileNameExt, ByRef $sExtension, ByRef $sExt)
    If StringInStr($sFilePath,"..") Then $sFilePath=_PathFull($sFilePath)
    Local $aPartOfPath=StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
;~  If @error Then
        ReDim $aPartOfPath[9]
;~      $aPartOfPath[0] = $sFilePath
;~  EndIf
    $aPartOfPath[0] = $sFilePath                       ; C:\Windows\System32\etc\hosts.exe
    $sDrive = $aPartOfPath[1]                          ; C:
    $sFullPathDir = $aPartOfPath[1] & $aPartOfPath[2]  ; C:\Windows\System32\etc
    If StringLeft($aPartOfPath[2], 1) == "/" Then
        $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\/")
    Else
        $sDirPath = StringRegExpReplace($aPartOfPath[2], "\h*[\/\\]+\h*", "\\")
    EndIf
    $aPartOfPath[2] = $sFullPathDir                 ; C:\Windows\System32\etc

    $sDirName=StringReplace($sDirPath,"\","")
    $sDirName=StringReplace($sDirPath,"/","")

    $sFileName = $aPartOfPath[3]                    ; hosts
    $aPartOfPath[5] = $sFileName                    ; hosts

    $sExtension = $aPartOfPath[4]                   ; .exe
    $aPartOfPath[7] = $sExtension                   ; .exe

    $aPartOfPath[3] = $sDirPath                     ; \Windows\System32\etc\
    $aPartOfPath[4] = $sDirName                     ; etc

    $aPartOfPath[6] = $sFileName & $sExtension      ; hosts.exe
    $sFileNameExt = $aPartOfPath[6]                 ; hosts.exe

    $sExt = StringReplace($sExtension,".","")   ; exe
    $aPartOfPath[8] = $sExt                         ; exe

    Return $aPartOfPath
EndFunc   ;==>_PathSplitByRef

 

Edited by Trong
code

Regards,
 

Share this post


Link to post
Share on other sites
InunoTaishou

Have you looked at _PathSplit?

Share this post


Link to post
Share on other sites
VIP

Same 

; #FUNCTION# ====================================================================================================================
; Author ........: Valik
; Modified.......: DXRW4E - Re-wrote to use a regular expression; guinness - Update syntax and structure.
; ===============================================================================================================================
Func _PathSplit($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
    If @error Then ; This error should never happen.
        ReDim $aArray[5]
        $aArray[0] = $sFilePath
    EndIf
    $sDrive = $aArray[1]
    If StringLeft($aArray[2], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\/")
    Else
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\\")
    EndIf
    $aArray[2] = $sDir
    $sFileName = $aArray[3]
    $sExtension = $aArray[4]
    Return $aArray
EndFunc   ;==>_PathSplit

but need

10 minutes ago, Trong said:

make working and better :)


Regards,
 

Share this post


Link to post
Share on other sites
InunoTaishou

Not sure I understand working and better when it literally does everything you're asking us to try and figure out.....

#include <Array.au3>
#include <File.au3>

Global $sDrive, $sDir, $sFileName, $sExtension

_ArrayDisplay(_PathSplit(@ScriptFullPath, $sDrive, $sDir, $sFileName, $sExtension))
_ArrayDisplay(_PathSplit("A:", $sDrive, $sDir, $sFileName, $sExtension))
_ArrayDisplay(_PathSplit("A:\B.c", $sDrive, $sDir, $sFileName, $sExtension))
_ArrayDisplay(_PathSplit("D:\E\F\", $sDrive, $sDir, $sFileName, $sExtension))
_ArrayDisplay(_PathSplit("G:\H/../J.k/", $sDrive, $sDir, $sFileName, $sExtension))
_ArrayDisplay(_PathSplit("M:\N\k..J.k", $sDrive, $sDir, $sFileName, $sExtension))
_ArrayDisplay(_PathSplit("D:\E\F\..\G\G\I..J.K.M", $sDrive, $sDir, $sFileName, $sExtension))

 

Edited by InunoTaishou

Share this post


Link to post
Share on other sites
VIP

_PathSplit()        :  $sFilePath, $sDrive, $sDir, $sFileName, $sExtension
_PathSplitNEW(): $sFilePath, $sDrive, $sFullPathDir, $sDirPath, $sDirName, $sFileName, $sFileNameExt, $sExtension, $sExt

Add: $sFullPathDir, $sDirName, $sFileNameExt, $sExt


Regards,
 

Share this post


Link to post
Share on other sites
InunoTaishou

$sFullPathDir is [0] (Unless you mean full path without the file then it's [1] & [2])

$sDirName is [2] unless you mean the last folder the file is in. In which case you can get that using StringTrimLeft, StringRight, or StringMid

$sFileNameExt and $sExt are [4]

#include <Array.au3>
#include <File.au3>

Global $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension

_ArrayDisplay(_PathSplitEx(@ScriptFullPath, $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension))
_ArrayDisplay(_PathSplitEx("A:", $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension))
_ArrayDisplay(_PathSplitEx("A:\B.c", $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension))
_ArrayDisplay(_PathSplitEx("D:\E\F\", $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension))
_ArrayDisplay(_PathSplitEx("G:\H/../J.k/", $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension))
_ArrayDisplay(_PathSplitEx("M:\N\k..J.k", $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension))
_ArrayDisplay(_PathSplitEx("D:\E\F\..\G\G\I..J.K.M", $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension))

Func _PathSplitEx($sPath, ByRef $sDrive, ByRef $sFullPathDir, ByRef $sDir, ByRef $sFolder, ByRef $sFileName, ByRef $sExtension)
    Local $aReturn = _PathSplit($sPath, $sDrive, $sDir, $sFileName, $sExtension)
    $sFullPathDir = $aReturn[1] & $aReturn[2]
    $sFolder = StringRight($aReturn[2], StringLen($aReturn[2]) - StringInStr($aReturn[2], "\", 0, -2))
    _ArrayInsert($aReturn, 2, $sFullPathDir)
    _ArrayInsert($aReturn, 4, $sFolder)
    Return $aReturn
EndFunc

 

Edited by InunoTaishou

Share this post


Link to post
Share on other sites
VIP

I want it better and use StringRegExp, your code too long

 

#include <Array.au3>
#include <File.au3>

_TEST("C:\Windows\System32\etc\hosts.exe")
_TEST("A:")
_TEST("A:\B.c")
_TEST("D:\E\F\")
_TEST("G:\H/../J.k/")
_TEST("M:\N\k..J.k")
_TEST("D:\E\F\..\G\G\I..J.K.M")

Func _TEST($sFilePath)
    Local $sDrive, $sDir, $sFileName, $sExtension, $sFullPathDir, $sDirName, $sFileNameExt, $sExt
    Local $aPathSplit = _PathSplitByRef($sFilePath, $sDrive, $sDir, $sFileName, $sExtension, $sFullPathDir, $sDirName, $sFileNameExt, $sExt)
    _ArrayDisplay($aPathSplit, "_PathSplit of " & $sFilePath)
EndFunc   ;==>_TEST

Func _PathSplitByRef($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension, ByRef $sFullPathDir, ByRef $sDirName, ByRef $sFileNameExt, ByRef $sExt)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
;~     If @error Then ; This error should never happen.
        ReDim $aArray[9]
        $aArray[0] = $sFilePath
;~     EndIf
    $sDrive = $aArray[1]
    If StringLeft($aArray[2], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\/")
        $sDirName = StringReplace(StringRight($sDrive & $sDir, StringInStr($sDrive & $sDir, "\") + 1), "/", "")
    Else
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\\")
        $sDirName = StringReplace(StringRight($sDrive & $sDir, StringInStr($sDrive & $sDir, "\") + 1), "\", "")
    EndIf
    $aArray[2] = $sDir
    $sFileName = $aArray[3]
    $sExtension = $aArray[4]
    $sFullPathDir = $sDrive & $sDir
    $aArray[5] = $sFullPathDir
    $aArray[6] = $sDirName
    $sFileNameExt = $sFileName & $sExtension
    $aArray[7] = $sFileNameExt
    $sExt = StringReplace($sExtension, ".", "")
    $aArray[8] = $sExt
    Return $aArray
EndFunc   ;==>_PathSplitByRef

 

Edited by Trong
...

Regards,
 

Share this post


Link to post
Share on other sites
InunoTaishou

My code's too long...? It's 4 extra lines...

Well maybe one of the more experienced RegEx experts can help you out then. Just know that in some instances, using String* functions are actually faster than a StringRegExp call.

Share this post


Link to post
Share on other sites
VIP

Your function working too slow! UDF it's not just 4 lines!
TEST SPEED:
Start process SpitPath1....Done on: 27804.4166587552
Start process SpitPath2....Done on: 116759.591921566

#include <Array.au3>
#include <File.au3>


Local $sFileList = @TempDir & "\~FileListALL.txt"
If Not FileExists($sFileList) Then
    ConsoleWrite("Get file list....")
    Local $StartTimeGetListFile = TimerInit()
    Local $aFileList = _FileListToArrayRec(@HomeDrive&"\", "*.*", 0, 1, 1, 2)
    Local $EndTimeGetListFile = TimerDiff($StartTimeGetListFile)
    ConsoleWrite("Done on: " & $EndTimeGetListFile & @CRLF)
    ConsoleWrite("Wrting file list!")
    Local $StartTimeSaveListFile = TimerInit()
    Local $sFileContent
    For $i = 1 To $aFileList[0] - 1
        $sFileContent &= $aFileList[$i] & @CRLF
    Next
    Local $sFileOpen = FileOpen($sFileList, 2)
    FileWrite($sFileOpen, $sFileContent)
    FileClose($sFileOpen)
    Local $EndTimeSveListFile = TimerDiff($StartTimeSaveListFile)
    ConsoleWrite("Done on: " & $EndTimeSveListFile & @CRLF)
EndIf

ConsoleWrite("Start process SpitPath1....")
Local $StartTimeProcess = TimerInit()
Local $aFileList = FileReadToArray($sFileList)
For $i = 0 To UBound($aFileList) - 1
    _TEST1($aFileList[$i])
Next
Local $EndTimeProcess = TimerDiff($StartTimeProcess)
ConsoleWrite("Done on: " & $EndTimeProcess & @CRLF)

ConsoleWrite("Start process SpitPath2....")
Local $StartTimeProcess = TimerInit()
Local $aFileList = FileReadToArray($sFileList)
For $i = 0 To UBound($aFileList) - 1
    _TEST2($aFileList[$i])
Next
Local $EndTimeProcess = TimerDiff($StartTimeProcess)
ConsoleWrite("Done on: " & $EndTimeProcess & @CRLF)

Func _TEST1($sFilePath)
    Local $sDrive, $sDir, $sFileName, $sExtension, $sFullPathDir, $sDirName, $sFileNameExt, $sExt
    Local $aPathSplit = _PathSplitByRef($sFilePath, $sDrive, $sDir, $sFileName, $sExtension, $sFullPathDir, $sDirName, $sFileNameExt, $sExt)
;~  _ArrayDisplay($aPathSplit, "_PathSplit of " & $sFilePath)
EndFunc   ;==>_TEST1

Func _TEST2($sFilePath)
    Local $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension
    Local $aPathSplit = _PathSplitEx($sFilePath, $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension)
EndFunc   ;==>_TEST2



Local $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension
Func _PathSplitEx($sPath, ByRef $sDrive, ByRef $sFullPathDir, ByRef $sDir, ByRef $sFolder, ByRef $sFileName, ByRef $sExtension)
    Local $aReturn = _PathSplit($sPath, $sDrive, $sDir, $sFileName, $sExtension)
    $sFullPathDir = $aReturn[1] & $aReturn[2]
    $sFolder = StringRight($aReturn[2], StringLen($aReturn[2]) - StringInStr($aReturn[2], "\", 0, -2))
    _ArrayInsert($aReturn, 2, $sFullPathDir)
    _ArrayInsert($aReturn, 4, $sFolder)
    Return $aReturn
EndFunc   ;==>_PathSplitEx

Func _PathSplitByRef($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension, ByRef $sFullPathDir, ByRef $sDirName, ByRef $sFileNameExt, ByRef $sExt)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
;~     If @error Then ; This error should never happen.
    ReDim $aArray[9]
    $aArray[0] = $sFilePath
;~     EndIf
    $sDrive = $aArray[1]
    If StringLeft($aArray[2], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\/")
        $sDirName = StringReplace(StringRight($sDrive & $sDir, StringInStr($sDrive & $sDir, "\") + 1), "/", "")
    Else
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\\")
        $sDirName = StringReplace(StringRight($sDrive & $sDir, StringInStr($sDrive & $sDir, "\") + 1), "\", "")
    EndIf
    $aArray[2] = $sDir
    $sFileName = $aArray[3]
    $sExtension = $aArray[4]
    $sFullPathDir = $sDrive & $sDir
    $aArray[5] = $sFullPathDir
    $aArray[6] = $sDirName
    $sFileNameExt = $sFileName & $sExtension
    $aArray[7] = $sFileNameExt
    $sExt = StringReplace($sExtension, ".", "")
    $aArray[8] = $sExt
    Return $aArray
EndFunc   ;==>_PathSplitByRef

 

Edited by Trong
CODE

Regards,
 

Share this post


Link to post
Share on other sites
InunoTaishou

Slight modification makes my Ex function only a few milliseconds slower than yours (using 22,061 files to split)

Func _PathSplitEx($sPath, ByRef $sDrive, ByRef $sFullPathDir, ByRef $sDir, ByRef $sFolder, ByRef $sFileName, ByRef $sExtension)
    Local $aReturn = _PathSplit($sPath, $sDrive, $sDir, $sFileName, $sExtension)
    $sFullPathDir = $aReturn[1] & $aReturn[2]
    $sFolder = StringRight($aReturn[2], StringLen($aReturn[2]) - StringInStr($aReturn[2], "\", 0, -2))
    Return StringSplit($sPath & "|" & $sDrive & "|" & $sFullPathDir & "|" & $sDir & "|" & $sFolder & "|" & $sFileName & "|" & $sExtension, "|", $STR_NOCOUNT)
EndFunc   ;==>_PathSplitEx

_ArrayInsert has a large overhead. In a real world practice it would never be noticed, since, honestly, no one is going to call _PathSplit thousands of times in a row. Creating a new string and returning the split version increases the call substantially. And, looks a hell of a lot cleaner.

  • Like 1

Share this post


Link to post
Share on other sites
VIP

Anyway thank you for your interest and reply!

For me speed and simplicity are always given priority. I will try to learn to optimize code more.
A program does not just use a single function.
If you work with computers/VPS with low profile you will see the difference.


 

#include <Array.au3>
#include <File.au3>


Local $sFileList = @TempDir & "\~FileListALL.txt"
;~ Local $sFileList = @TempDir & "\~FileListTMPA.txt"
If Not FileExists($sFileList) Then
    ConsoleWrite("-Get file list....")
    Local $StartTimeGetListFile = TimerInit()
;~  Local $aFileList = _FileListToArrayRec(@HomeDrive & "\", "*.*", 0, 1, 1, 2)
;~  Local $aFileList = _FileListToArrayRec(@AppDataCommonDir & "\", "*.*", 0, 1, 1, 2)
    Local $EndTimeGetListFile = TimerDiff($StartTimeGetListFile)
    ConsoleWrite("Done on: " & $EndTimeGetListFile & @CRLF)
    ConsoleWrite("-Wrting file list!")
    Local $StartTimeSaveListFile = TimerInit()
    Local $sFileContent
    For $i = 1 To $aFileList[0] - 1
        $sFileContent &= $aFileList[$i] & @CRLF
    Next
    Local $sFileOpen = FileOpen($sFileList, 2)
    FileWrite($sFileOpen, $sFileContent)
    FileClose($sFileOpen)
    Local $EndTimeSveListFile = TimerDiff($StartTimeSaveListFile)
    ConsoleWrite("Done on: " & $EndTimeSveListFile & @CRLF)
EndIf

ConsoleWrite("+Read data ....")
Local $StartTimeRead = TimerInit()
Local $aFileList = FileReadToArray($sFileList)
Local $EndTimeRead = TimerDiff($StartTimeRead)
ConsoleWrite("Done on: " & $EndTimeRead & @CRLF)

ConsoleWrite("-Start process SpitPath1....")
Global $sDrive, $sDir, $sFileName, $sExtension, $sFullPathDir, $sDirName, $sFileNameExt
Local $StartTimeProcess = TimerInit()
For $i = 0 To UBound($aFileList) - 1
    _TEST1($aFileList[$i])
Next
Local $EndTimeProcess = TimerDiff($StartTimeProcess)
ConsoleWrite("Done on: " & $EndTimeProcess & @CRLF)

ConsoleWrite("-Start process SpitPath2....")
Global $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension
Local $StartTimeProcess = TimerInit()
For $i = 0 To UBound($aFileList) - 1
    _TEST2($aFileList[$i])
Next
Local $EndTimeProcess = TimerDiff($StartTimeProcess)
ConsoleWrite("Done on: " & $EndTimeProcess & @CRLF)

Func _TEST1($sFilePath)
;~  Local $aPathSplit =
    Return _PathSplitByRef($sFilePath, $sDrive, $sDir, $sFileName, $sExtension, $sFullPathDir, $sDirName, $sFileNameExt)
;~  _ArrayDisplay($aPathSplit, "_PathSplit of " & $sFilePath)
EndFunc   ;==>_TEST1

Func _TEST2($sFilePath)
;~  Local $aPathSplit =
    Return _PathSplitEx($sFilePath, $sDrive, $sFullPathDir, $sDir, $sFolder, $sFileName, $sExtension)
EndFunc   ;==>_TEST2

Func _PathSplitEx($sPath, ByRef $sDrive, ByRef $sFullPathDir, ByRef $sDir, ByRef $sFolder, ByRef $sFileName, ByRef $sExtension)
    Global $aReturn = _PathSplit($sPath, $sDrive, $sDir, $sFileName, $sExtension)
    $sFullPathDir = $aReturn[1] & $aReturn[2]
    $sFolder = StringRight($aReturn[2], StringLen($aReturn[2]) - StringInStr($aReturn[2], "\", 0, -2))
    Return StringSplit($sPath & "|" & $sDrive & "|" & $sFullPathDir & "|" & $sDir & "|" & $sFolder & "|" & $sFileName & "|" & $sExtension, "|", $STR_NOCOUNT)
EndFunc   ;==>_PathSplitEx


Func _PathSplitByRef($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension, ByRef $sFullPathDir, ByRef $sDirName, ByRef $sFileNameExt)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
;~     If @error Then ; This error should never happen.
    ReDim $aArray[8]
    $aArray[0] = $sFilePath
;~     EndIf
    $sDrive = $aArray[1]
    If StringLeft($aArray[2], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\/")
        $sDirName = StringReplace(StringRight($sDrive & $sDir, StringInStr($sDrive & $sDir, "\") + 1), "/", "")
    Else
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\\")
        $sDirName = StringReplace(StringRight($sDrive & $sDir, StringInStr($sDrive & $sDir, "\") + 1), "\", "")
    EndIf
    $aArray[2] = $sDir
    $sFileName = $aArray[3]
    $sExtension = $aArray[4]
    $sFullPathDir = $sDrive & $sDir
    $aArray[5] = $sFullPathDir
    $aArray[6] = $sDirName
    $sFileNameExt = $sFileName & $sExtension
    $aArray[7] = $sFileNameExt
    Return $aArray
EndFunc   ;==>_PathSplitByRef

 


Regards,
 

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

  • Similar Content

    • therks
      By therks
      So I have this pattern: 
      ^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$ And I'm expecting (depending on input) to get a 1, 2 or 3 index array (or @error for invalid input).
      But instead I get this:
      #include <Debug.au3> Func Test($String) _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$', 1)) EndFunc Test('10') ; Results (normal, expected): ; Row 0|10 Test('10:20') ; Results (extra blank index): ; Row 0| ; Row 1|10 ; Row 2|20 Test('10:20:30') ; Results (three blank indices): ; Row 0| ; Row 1| ; Row 2| ; Row 3|10 ; Row 4|20 ; Row 5|30 Is this normal? Should I just code around it, or is there a better way to do what I'm looking for?
      I also tried reversing my regex, but it was even uglier results:
      #include <Debug.au3> Func Test($String) _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+):(\d+):(\d+))|(\d+):(\d+)|(\d+)$', 1)) EndFunc Test('10') ; Results (yuck): ; Row 0| ; Row 1| ; Row 2| ; Row 3| ; Row 4| ; Row 5|10 Test('10:20') ; Results (slightly better): ; Row 0| ; Row 1| ; Row 2| ; Row 3|10 ; Row 4|20 Test('10:20:30') ; Results (nice): ; Row 0|10 ; Row 1|20 ; Row 2|30  
    • Deye
      By Deye
      Hi,
      I want to add any needed conditions to the StringRegExp command so it can pull out only  "File.au3", "WinAPIFiles.au3", "Test.bmp" into the array
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include 'WinAPIFiles.au3' #include "File.au3" ; Script Start - Add your code below here Local $bFileInstall = False ; Change to True and ammend the file paths accordingly. ; This will install the file C:\Test.bmp to the script location. If $bFileInstall Then FileInstall("C:\Test.bmp", @ScriptDir & "\Test.bmp") $sFile = FileRead(@ScriptFullPath) $aResults = StringRegExp($sFile, "(?i)(FileInstall\s*|include\s*)(.*)", 3) _ArrayDisplay($aResults) Thanks In Advance
      Deye
    • luckyluke
      By luckyluke
      $t = '... 1-347-318-9643 1-347-318-9647 1-347-318-9648 1-347-318-9650 1-347-318-9651 1-347-318-9652 1-347-318-9653 1-347-318-9655 1-347-318-&nbsp;...' $pattern = '347.*?318.*?9655' $tmp = StringRegExpReplace($t, $pattern, "|||", 1) ConsoleWrite($tmp & @CRLF) However i got this output:
      ... 1-|||  1-347-318-&nbsp;...
      Why i got only that, where is the other string, i thought the output should be this:
      ... 1-347-318-9643  1-347-318-9647  1-347-318-9648  1-347-318-9650  1-347-318-9651  1-347-318-9652  1-347-318-9653  1-|||  1-347-318-&nbsp;...
    • FroVN
      By FroVN
      i have a text : <Name>Jonh</Name>.<Age>15</Age>
      how i can get Jonh and 15 in one stringregexp? pls give me example
    • therks
      By therks
      I'm looking for a regex genius, cus I'm stumped when it comes to assertions.
      So what I have now, is this regular expression: ([^|=]+)=([^|]+)
      It takes a string (user input) of keys=values separated by pipes (ie: "param=value|param=value") and splits them into an array.
      Example:
      $vParamData = 'example=value|fruit=apple|phrase=Hello world' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; Result ; [0] => example ; [1] => value ; [2] => fruit ; [3] => apple ; [4] => phrase ; [5] => Hello world So that's working fine, but I'm wondering if there's also a way I could have this capture escaped pipes instead of splitting by them.
      ie:
      $vParamData = 'pipe test=this \| is a pipe|example=value' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; I'm getting this: ; [0] => pipe test ; [1] => this \ ; [2] => example ; [3] => value ; But I'd like a result like this: ; [0] => pipe test ; [1] => this \| is a pipe ; [2] => example ; [3] => value Is there some pattern that would accomplish this, or am I better off parsing it some other way?
×