Search the Community
Showing results for tags '_pathsplit parent'.
-
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