Sign in to follow this  
Followers 0
CSP

A function to get a relative path from A to B

5 posts in this topic

I'm looking for a way to get a relative path between two folders if two absolute paths were given. Does anyone know of one?

Also, is there a way to run a search on function descriptions? It seems like most of my questions will be about asking for the names of functions that does X. I'd like to figure it out myself without bothering you guys, but I can't seem to find a way to search the function docs.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm looking for a way to get a relative path between two folders if two absolute paths were given. Does anyone know of one?

Also, is there a way to run a search on function descriptions? It seems like most of my questions will be about asking for the names of functions that does X. I'd like to figure it out myself without bothering you guys, but I can't seem to find a way to search the function docs.

I have created this UDF and use it in AutoIt3Wrapper_GUI()

Func DirGetRelativePath($Source,$target)
; This UDF will get the relative Path to the file ... written by JdeB
; ConsoleWrite('>$Source = ' & $Source & '  $target = ' & $target & @crlf )
    $Source = StringReplace($Source,"/","\")
    $target = StringReplace($target,"/","\")
    Local $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt
    Local $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt
    _PathSplit($Source, $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt)
    _PathSplit($target, $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt)
    If $sz_sDrive <> $sz_tDrive then 
        return $target
    EndIf
;
    Local $a_sDir = StringSplit($sz_sDir,"\")
    Local $a_tDir = StringSplit($sz_tDir,"\")
    For $x = 1 to $a_sDir[0]
        If $a_sDir[$x] <> $a_tDir[$x] Then ExitLoop
    Next
    Local $R_Path = ""
    If $a_sDir[0] >= $a_tDir[0] Then
        For $y = $x to $a_sDir[0] -1
            $R_Path &= "..\"
        Next
        For $y = $x to $a_tDir[0] -1
            $R_Path &= $a_tDir[$y] & "\"
        Next
        $R_Path &= $sz_tFName & $sz_tExt
    Else
        For $y = $x to $a_tDir[0] -1
            $R_Path &= $a_tDir[$y] & "\"
        Next
        $R_Path &= $sz_tFName & $sz_tExt
    EndIf
    return $R_Path
EndFunc
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I have created this UDF and use it in AutoIt3Wrapper_GUI()

Thanks, that works.

One small note is that this function seems to require a trailing \ or / at the end of target path.

IE, "c:\windows\system\" => "c:\windows\system32" will result in "..\system32"

BUT, "c:\windows\system" => "c:\windows\system32" seems to result in "system32"

Nonetheless, thank you.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I found a bug if the source dir is totally different than the target dir and its part length is shorter, ie ("C:\Windows", "C:\Users\name\one"). I've modified it a bit, and I think it works correctly under all circumstances now. Any trailing "\" are removed from both source and target first, and the result lacks the preceding "\" as well.

Thanks to Jos for the original function!

Think I got it this time -

; function by Jos, modified by wraithdu
Func _DirGetRelativePath($source, $target)
; This UDF will get the relative Path to the file ... written by JdeB
    $source = StringReplace($source,"/","\")
    $target = StringReplace($target,"/","\")
    If StringRight($source, 1) == "\" Then $source = StringTrimRight($source, 1)
    If StringRight($target, 1) == "\" Then $target = StringTrimRight($target, 1)
;~  ConsoleWrite('->$source = ' & $source & '    $target = ' & $target & @crlf )
    Local $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt
    Local $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt
    _PathSplit($source, $sz_sDrive, $sz_sDir, $sz_sFName, $sz_sExt)
    _PathSplit($target, $sz_tDrive, $sz_tDir, $sz_tFName, $sz_tExt)
    If $sz_sDrive <> $sz_tDrive Then
        Return $target
    EndIf
    
    Local $a_sDir = StringSplit($sz_sDir,"\")
    Local $a_tDir = StringSplit($sz_tDir,"\")
    For $x = 1 to $a_sDir[0]
        If $a_sDir[$x] <> $a_tDir[$x] Then ExitLoop
    Next
    If $x > $a_sDir[0] Then $x -= 1
    Local $R_Path = ""
    If $a_sDir[0] >= $a_tDir[0] Or $x + 3 >= $a_sDir[0] Then
        For $y = $x to $a_sDir[0]
            $R_Path &= "..\"
        Next
        For $y = $x to $a_tDir[0] - 1
            $R_Path &= $a_tDir[$y] & "\"
        Next
        $R_Path &= $sz_tFName & $sz_tExt
    Else
        $x += 1
        For $y = $x to $a_tDir[0] - 1
            $R_Path &= $a_tDir[$y] & "\"
        Next
        $R_Path &= $sz_tFName & $sz_tExt
    EndIf
    return $R_Path
EndFunc
Edited by wraithdu

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  
Followers 0