VIP

[Help] Path Split WITH StringRegExp

11 posts in this topic

#1 ·  Posted (edited)

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



#2 ·  Posted

Have you looked at _PathSplit?

Share this post


Link to post
Share on other sites

#3 ·  Posted

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

#4 ·  Posted (edited)

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

#5 ·  Posted

_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

#6 ·  Posted (edited)

$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

#7 ·  Posted (edited)

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

#8 ·  Posted

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

#9 ·  Posted (edited)

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

#10 ·  Posted

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.

1 person likes this

Share this post


Link to post
Share on other sites

#11 ·  Posted

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

    • Robinson1
      By Robinson1
      Well the plan is to use the power of regular expressions engine of AutoIT for patching binary data.
      Something like this: StringRegExp( $BinaryData,  "(?s)\x55\x8B.."
       
      <cut> ... Okay straight to question/problem
      ... certain bytes that are in the range from 0x80 to 0xA0 won't match.
      Hmm seem to be a char encoding problem. In detail these are 27 chars: 0x80, 0x82~8C, 0x8E, 0x91~9C, 0x9E,0x9F
      Here's a small code snippet to explore / explain this problem:
      #include "StringConstants.au3" $TestData = BinaryToString("0x7E7F808182") ;Okay $match = StringRegExp( $TestData ,'\x7E' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x7F' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x80' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x81' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x82' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;~ output: ;~ @extended = 2 $match = ;~ @extended = 3 $match = ;~ @extended = 0 $match = 1 ;~ @extended = 5 $match = ;~ @extended = 0 $match = 1 Hmm what to do? Go back and use the 'numberstring monster' implementation or just omit that range of 'unsafe bytes'. What is the root of this problem?
      Any idea how to fix this?
       
      Update: Okay I know a byte is not a character.
      But StringRegExp operates on String and so character level.
      Okay as long as you stay at Ansi encoding and only use /x00 - /X7F in the search pattern using  StringRegExp works well to search for binary data.
      What bytes can be matched that are in the range from /X7F - /xFF is also depending on the code page.
      So this avoid to search for bytes in the range from 0x80-0xa0 only applies to Germany.
      I just change this country setting:

      to Thai and now near all bytes from /X7F - /xFF fails to match.
    • Carm01
      By Carm01
      Hello,
      I have spent the past day fooling with StringRegExp to no avail attempting to get what would be a simple solution to an issue using StringRegExp.
      I will post the code in a sec. The string 'Java x Update y' where x and y are numeric values ONLY if a letter is mixed in anywhere then it should fail. I have been able to successfully deal with the x value so if x = 1234 or a1234 or 1a234 or 1234a would result in a fail if 'a' was in the string. However, when y = 1a234 then I get an output of 1 and when y = 1234a then the output = 1234 when both should fail. I am probably overlooking something simple and in looking through all the material and experimenting I am unable to figure it out and my experience with stringregexp and trying to find examples of this proved difficult. If someone could assist or point me to a thread ? Here is my code ; prob a simple fix. I am also trying to avoid white spaces.
      Thanks in advance
      #include <array.au3> $aArray = StringRegExp('Java 3009 Update 1a21', '(?i)Java (\d+) Update (\d+)', $STR_REGEXPARRAYGLOBALMATCH) If @error Then Exit _ArrayDisplay($aArray)  
    • ViciousXUSMC
      By ViciousXUSMC
      So I ran into this crazy "program" that cant be uninstalled via WMI, MSIExec, etc.
      The only way to uninstall it was from Add/Remove programs manually... Or I found if you find it in the registry under HKCU and run the  uninstall string, it will also uninstall.
      However the string in the registry cant be run directly in a cmd window because of the format errors.
      It has spaces without quotations, it has invalid characters, etc, etc 
      I know things run different when executed in the registry, so maybe there is a way I can run the regsitry key just like how the system does?  If so chime in.
      Otherwise I did this a crude way using several stringregexpreplace() functions and have it working.
      The solution feels so barbaric and crude that I wanted to post it so some of you guys better than me can clean up the code, maybe offer alternative ways to do it, or reduce the number of times I process the string.
      Here is the string right out of the registry:
      c:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\VSTOInstaller.exe /Uninstall file:///C:/Users/it022565/AppData/Local/Temp/OOBAXTOWordAddIn/ApplicationXtender.AXTO.Word.vsto Here is my cave man scripting to turn this into a run able string.
       
      Func _UninstallOld() For $i = 1 to 100 ;Enumerate Registry $sEnumBase = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" ;Look in HKCU for the uninstall string for the old version $sEnum = RegEnumKey($sEnumBase, $i) If @Error Then Return If $iDebug = 1 Then MsgBox(0, "", $sEnum) If StringInStr(RegRead($sEnumBase & $sEnum, "DisplayName"), "Word Addin") Then ExitLoop Next If $iDebug = 1 Then MsgBox(0, "", $sEnum) $sKey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $sEnum $sKey2 = RegRead($sKey, "UninstallString") If $iDebug = 1 Then MsgBox(0, "Original Install Location", $sKey2) $sKey3 = StringRegExpReplace($sKey2, "(?i)(c:.*exe)", '"$1"') If $iDebug = 1 Then MsgBox(0, "", $sKey3) $sKey4 = StringRegExpReplace($sKey3, "(?i)file:///", "") If $iDebug = 1 Then MsgBox(0, "", $sKey4) $sKey5 = StringRegExpReplace($sKey4, "%20", " ") If $iDebug = 1 Then MsgBox(0, "", $sKey5) $sKey6 = StringRegExpReplace($sKey5, '(?i)((?<!")c:.*vsto)', '"$1"') If $iDebug = 1 Then MsgBox(0, "", $sKey6) RunWait(@ComSpec & ' /c ' & '"' & $sKey6 & ' /s"', "", @SW_HIDE) EndFunc Basically step by step I add quotations, strip bad characters, etc.  Kind of proud for using look behind for once
      Looking forward to what you guys come up with.
    • Subz
      By Subz
      Does anyone know how to split a string using multiple delimiters, returning both the values and delimiters withing the Array using StringRegExp?  For example:
      ;~ Split on " Not ", " And ", " Or " $sString = ' Not $a = 1 And $b = 2 Or $b = 3' $aArray = StringRegExp($sString,...) ;~ Returned Results $aArray[0] = '$a = 1' $aArray[1] = 'And' $aArray[2] = '$b = 2' $aArray[3] = 'Or' $aArray[4] = '$b = 3' At the moment I'm using
      Local $aArray1 = StringRegExp($sString, '(?i) Or | And | Not ', 3) Creating a new array using string split and then joining the two arrays together again
      Local $aArray1 = StringSplit(StringRegExpReplace($sString, '(?i) Or | And | Not ', '******'), '******', 3) Unfortunately regular expression isn't my forte.
    • VIP
      By VIP
      Hi,
      I need help string RegEx to get string from CREATE to GO
      #include <StringConstants.au3> ;~ Global $fileSQL1 = @ScriptDir & "\fileSQL1.sql" ;~ Global $fileSQL2 = @ScriptDir & "\fileSQL2.sql" Global $tmpSQLfile = @TempDir & "\tmpFile.sql" OnAutoItExitRegister("_OnExit") _SetTMPsql() If Not FileExists($tmpSQLfile) Then OnAutoItExitUnRegister("_OnExit") Exit MsgBox(48, "/!\", "File: " & $tmpSQLfile & @CRLF & " is not Exists!", 3) EndIf Global $ContentSQLfile = FileRead($tmpSQLfile) _Start() Func _Start() Local $aArray, $iOffset = 1, $stringRegExp = '(?i)CREATE(.*?)GO' While 1 $aArray = StringRegExp($ContentSQLfile, $stringRegExp, $STR_REGEXPARRAYMATCH, $iOffset) If @error Then MsgBox(48, "StringRegExp Error " & @error, "+> StringRegExp: " & $stringRegExp & @CRLF & @CRLF & "=> With STRING:" & @CRLF & @CRLF & $ContentSQLfile) ExitLoop EndIf $iOffset = @extended For $i = 0 To UBound($aArray) - 1 MsgBox(0, "RegExp Test with Option 1 - " & $i, $aArray[$i]) Next WEnd EndFunc ;==>_Start Func _SetTMPsql() Local $tmpSQLContent = "" $tmpSQLContent &= "USE [Master]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "CREATE DATABASE [Sales] ON PRIMARY " & @CRLF $tmpSQLContent &= "( NAME = N’Sales’, FILENAME = N’\FSASQLDBSales.mdf’ , " & @CRLF $tmpSQLContent &= " SIZE = 2GB , MAXSIZE = 8GB, FILEGROWTH = 1GB )" & @CRLF $tmpSQLContent &= "LOG ON " & @CRLF $tmpSQLContent &= "( NAME = N’Sales_log’, FILENAME = N’\FSASQLDBSales_log.ldf’ , " & @CRLF $tmpSQLContent &= " SIZE = 1GB , MAXSIZE = 2GB , FILEGROWTH = 10%)" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "USE [Sales]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- Table Product" & @CRLF $tmpSQLContent &= "CREATE TABLE [dbo].[Product]" & @CRLF $tmpSQLContent &= "(" & @CRLF $tmpSQLContent &= " [ProductId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF $tmpSQLContent &= " [ProductName] [nchar](50) NULL," & @CRLF $tmpSQLContent &= " [ProductDescription] [nchar](3000) NULL," & @CRLF $tmpSQLContent &= " [ProductPrice] MONEY NULL" & @CRLF $tmpSQLContent &= ") ON [PRIMARY]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- Table Sales" & @CRLF $tmpSQLContent &= "CREATE TABLE [dbo].[Sales]" & @CRLF $tmpSQLContent &= "( " & @CRLF $tmpSQLContent &= " [SaleId] [uniqueidentifier] DEFAULT NEWID() NOT NULL," & @CRLF $tmpSQLContent &= " [SaleName] [nchar](50) NULL," & @CRLF $tmpSQLContent &= " [SaleInfo] [nchar](3000) NULL," & @CRLF $tmpSQLContent &= " [SaleMoney] MONEY NULL" & @CRLF $tmpSQLContent &= ") ON [PRIMARY]" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "SET ANSI_NULLS ON" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "SET QUOTED_IDENTIFIER ON" & @CRLF $tmpSQLContent &= "GO" & @CRLF $tmpSQLContent &= "" & @CRLF $tmpSQLContent &= "-- The End" & @CRLF Local $hOpen = FileOpen($tmpSQLfile, 2 + 8 + 128) FileWrite($hOpen, $tmpSQLContent) Return FileClose($hOpen) EndFunc ;==>_SetTMPsql Func _OnExit() Exit FileDelete($tmpSQLfile) EndFunc ;==>_OnExit  
      mikell