Jump to content

Last folder in a Path


rootx
 Share

Recommended Posts

Hi I have a folder like C:\route\luglio\40.714224,-73.961452

I would like to rename the last folder, so i built this method, that works, i would like to know if you have other options. Thx

 

$path = "C:\route\luglio\40.714224,-73.961452"
$slash = StringReplace($path,"\","\")
$Cslash = @extended
$extracted = StringLeft($path, StringInStr($path,"\",0,$Cslash))
ConsoleWrite($extracted)

$delim = "\"
$Left = StringLeft($path,StringInStr($path, $delim,0,$Cslash))
$Right = StringMid($path,StringInStr($path, $delim,0,$Cslash) + StringLen($delim))
ConsoleWrite($Left&"    "&$Right)

 

Edited by rootx
Link to comment
Share on other sites

Maybe :

May be helpful for you:

Local $sDrive, $sDir, $sFileName, $sExtension, $sDirPath, $sParentDirName, $sFileNameExt

Local $sFilePath = "C:\route\luglio\40.714224,-73.961452"
Local $aPathSplit = _SplitPathRef($sFilePath, $sDrive, $sDir, $sFileName, $sExtension, $sDirPath, $sParentDirName, $sFileNameExt)
ConsoleWrite("+ Parent Dir Name: " & _SplitPath($sFilePath, 6) & @CRLF& @CRLF)

ConsoleWrite("!Path IN       : " & $sFilePath & @CRLF) ; C:\Windows\System32\etc\hosts.exe
ConsoleWrite("- Driver       : " & $sDrive & @CRLF) ; C:
ConsoleWrite("- Dir          : " & $sDir & @CRLF) ; \Windows\System32\etc\
ConsoleWrite("- FileName     : " & $sFileName & @CRLF) ; hosts
ConsoleWrite("- Extension    : " & $sExtension & @CRLF) ; .exe
ConsoleWrite("- DirPath      : " & $sDirPath & @CRLF) ; C:\Windows\System32\etc\etc
ConsoleWrite("- ParentDirName: " & $sParentDirName & @CRLF) ; etc
ConsoleWrite("- FileNameExt  : " & $sFileNameExt & @CRLF & @CRLF) ; hosts.exe

; * -----:|
Func _SplitPath($sFilePath, $rType = 0)
    Local $sDrive, $sDir, $sFileName, $sExtension, $sDirPath, $sParentDirName, $sFileNameExt
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", 1)
;~  If @error Then ; This error should never happen.
    ReDim $aArray[9]
    $aArray[0] = $sFilePath
    $sDrive = $aArray[1]
;~  EndIf
    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]
    $sParentDirName = StringRegExpReplace($sFilePath, '\\[^\\]*$', '')
    $sParentDirName = StringRegExpReplace($sParentDirName, '.*\\', '')
    $aArray[5] = $sParentDirName
    $sFileNameExt = $sFileName & $sExtension
    $aArray[6] = $sFileNameExt
    $sDirPath = $sDrive & $sDir
    $aArray[7] = $sDirPath
    If $rType = 1 Then Return $sDrive
    If $rType = 2 Then Return $sDir
    If $rType = 3 Then Return $sFileName
    If $rType = 4 Then Return $sExtension
    If $rType = 5 Then Return $sDirPath
    If $rType = 6 Then Return $sParentDirName
    If $rType = 7 Then Return $sFileNameExt
    Return $aArray
EndFunc   ;==>_SplitPath
; * -----:| Dao Van Trong - TRONG.WIN
Func _SplitPathRef($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension, ByRef $sDirPath, ByRef $sParentDirName, ByRef $sFileNameExt)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", 1)
;~  If @error Then ; This error should never happen.
    ReDim $aArray[9]
    $aArray[0] = $sFilePath
    $sDrive = $aArray[1]
;~  EndIf
    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]
    $sParentDirName = StringRegExpReplace($sFilePath, '\\[^\\]*$', '')
    $sParentDirName = StringRegExpReplace($sParentDirName, '.*\\', '')
    $aArray[5] = $sParentDirName
    $sFileNameExt = $sFileName & $sExtension
    $aArray[6] = $sFileNameExt
    $sDirPath = $sDrive & $sDir
    $aArray[7] = $sDirPath
    Return $aArray
EndFunc   ;==>_SplitPathRef
; * -----:| Dao Van Trong - TRONG.WIN

 

Regards,
 

Link to comment
Share on other sites

I Rube-Goldberged your solution :)

$path = "C:\route\luglio\40.714224,-73.961452"
$test = "test---"

Msgbox(0,"", stringreverse(StringRegExpReplace(stringreverse($path), '\\' , stringreverse($test) & '\\' ,1)))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Also with _StringInsert :

#Include <String.au3>

$path = "C:\route\luglio\40.714224,-73.961452"
$test = "test---"

MsgBox(0, "", _StringInsert($path, $test, StringInStr($path, "\", 0, -1) ))
; MsgBox(0, "", StringRegExpReplace($path, ".+\\\K", $test) )

 

Link to comment
Share on other sites

; detemine the actual local folder
;ConsoleWrite("@ScriptDir " & @ScriptDir & @CRLF)
Local $path = StringSplit(@ScriptDir, "\")
;_ArrayDisplay($path)
Local $lastfoldercount = UBound($path, 1) - 1
;ConsoleWrite("$lastfoldercount " & $lastfoldercount & @CRLF)
Local $lastfolder = $path[$lastfoldercount]
;ConsoleWrite("$lastfolder " & $lastfolder & @CRLF)

 

Skysnake

Why is the snake in the sky?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...