Jump to content
Sign in to follow this  
VIP

[Help] Path Split WITH StringRegExp

Recommended Posts

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

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

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

_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

$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

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

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

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

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.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By FrancescoDiMuro
      Good morning
      I'm playing with SRE and trying to obtain some information from a test file.
      I was testing the pattern on regex101, but when I bring it to AutoIt, it doesn't return the same result as on regex101.
      I am surely (?:missing some important notes about PCRE engine|the pattern is not correct at all).
      Script:
      #include <Array.au3> #include <StringConstants.au3> Test() Func Test() Local $strFileName = @ScriptDir & "\TestFile.txt", _ $strFileContent, _ $arrResult $strFileContent = FileRead($strFileName) If @error Then Return ConsoleWrite("FileRead ERR: " & @error & @CRLF) $arrResult = StringRegExp($strFileContent, '(?sx)User:\h([^\n]+)\n' & _ 'Login\-name:\h([^\n]+)\n' & _ '(?:CaseSensitive:\h([^\n]+)\n)?' & _ 'NTSecurity:\h([^\n]+)\n' & _ '(?:NO\n)?' & _ '(?:Domain:\h([^\n]+)\n)?' & _ 'Timeout:\h([^\n]+)\n' & _ '.*?' & _ 'Member:\h([^\n]+)\n', $STR_REGEXPARRAYGLOBALMATCH) If IsArray($arrResult) Then _ArrayDisplay($arrResult) EndFunc Test file:
      User: AMMINISTRATORE Login-name: ADM CaseSensitive: YES NTSecurity: NO NO Timeout: 00:05:00 Member: AMMINISTRATORI User: Test_User Login-name: Test_User NTSecurity: YES Domain: DNEU Timeout: 00:00:00 Member: OPERATORS Member: OPERATORS Any help (even from cats) it's highly appreciated.

      Cheers
    • By ni3dprint
      Hi folks, 
      Thank you so much in advance for your help!  I've been using AUTOIT for manipulating gcode.  So far I've just worked through the excellent help examples and although I'm sure the resulting code is clumsy it has functioned  
      However now I'm trying to improve and advance things and I've stumbled across REGEX.. and I'm a bit stuck.  What I would like to be able to do is to 'move'/'transform' the gcode in a file and re-write it to a new file.  I only need to move it in one direction(X).  At the heart of this I need a script to extract all the X values and then ADD or SUBTRACT an adjustment factor to transform and rewrite the code accordingly.
      So far using an example script and an example input -
      Func Test2()
      Local $iMove = -4
          Local $sInput = '"G1 X45.036 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925"'
          Local $sOutput = StringRegExpReplace($sInput, '(?<=[X])\d+.\d+', '\0')
          Display($sInput, $sOutput)
      EndFunc   ;==>Test2
      This identifies the correct values i.e 45.036 and 48.036 but is there a way to dyamically adjust them before they are replaced, by for example a factor of -4 ($iMove above).  So far I can't seem to do math on the '\0' value i.e '\0'+ -4 ?
      Many thanks for your time and expertise!
       
    • By jmp
      i am trying to get number from string using this code :
      #include <IE.au3> $oIE = _IEAttach ("Edu.corner") Local $aName = "Student name & Code:", $iaName = "0" Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.InnerText = $aName Then $iaName = $oTd.NextElementSibling.InnerText $iGet = StringRegExpReplace($iaName, "\D", "") EndIf Next MsgBox(0, "", $iGet) it was get number like 52503058
      But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

       
    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
×
×
  • Create New...