youtuber Posted May 16, 2021 Share Posted May 16, 2021 As in the example, I just want to get the C:\Program Files (x86)\AutoIt3\ directory with the pattern. and how should I do this without StringReplace($ReplaceDirInEXE, "SciTE\", "") This code works fine for me, but the autoit directory is just an example, so I don't know what to replace SciTE \. $dir = "C:\Program Files (x86)\AutoIt3\SciTE\SciTE.exe" $ReplaceDirInEXE = StringRegExpReplace($dir, "[\w -.%]+(?=.exe)\.\w{2,3}", "$1") $ReplaceDirSciTE = StringReplace($ReplaceDirInEXE, "SciTE\", "") ConsoleWrite($ReplaceDirSciTE & @CRLF) Link to comment Share on other sites More sharing options...
Deye Posted May 16, 2021 Share Posted May 16, 2021 (edited) you want to get the path without the file name right ? $dir =StringRegExpReplace($dir, "(.*)(\\.*)", "\1") And if you need to climb an extra directory from the file name $dir =StringRegExpReplace($dir, "(.*)(\\.*){2}", "\1") Edited May 16, 2021 by Deye Link to comment Share on other sites More sharing options...
Musashi Posted May 16, 2021 Share Posted May 16, 2021 Looks a bit enormous at first glance, but I like @guinness 's solution. It covers various scenarios, including the one you are looking for. see : https://www.autoitscript.com/forum/topic/147654-_pathsplit-with-parent-folder/ Here is the script with examples (in case the link should ever get lost ) : Spoiler expandcollapse popup#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 _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 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 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
youtuber Posted May 16, 2021 Author Share Posted May 16, 2021 @Deye Thanks this works fine for me 👍🏻 And if you need to climb an extra directory from the file name $dir =StringRegExpReplace($dir, "(.*)(\\.*){2}", "\1") Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now