Jump to content
rootx

Last folder in a Path

Recommended Posts

rootx

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

Share this post


Link to post
Share on other sites
Danp2

This revision gives the same results --

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

$pos = StringInStr($path, $delim, 0, -1)
$Left = StringLeft($path, $pos)
$Right = StringMid($path, $pos + 1)
ConsoleWrite($Left&"    "&$Right & @CRLF)

P.S. What didn't work for you with _pathsplit?

Share this post


Link to post
Share on other sites
VIP

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

 

  • Like 1

Regards,
 

Share this post


Link to post
Share on other sites
rootx

P.S. What didn't work for you with _pathsplit?

I meant a similar function, _pathsplit work as well but not to resolve my question.

Share this post


Link to post
Share on other sites
mikell

Did you mean something like this ?

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

Msgbox(0,"", StringRegExpReplace($path, '.+\\\K(?=[^\\]+$)', $test) )

 

  • Like 1

Share this post


Link to post
Share on other sites
iamtheky

I Rube-Goldberged your solution :)

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

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

 

  • Like 1

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

Share this post


Link to post
Share on other sites
jguinch

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) )

 

  • Like 2

Share this post


Link to post
Share on other sites
Skysnake
; 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)

 

  • Like 1

Skysnake

Why is the snake in the sky?

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.