Jump to content
Server Maintenance This Week. ×

Get Relative Path


This topic is 4889 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I'm looking for a function that will generate a relative path from one file path to another. for example:

GetRelativePath ( "\\TYPELEVEN-HOME\backups\pls\test.wpl" ; "\\TYPELEVEN-HOME\backups\pls\LogMeIn.msi" ) = "LogMeIn.msi"

GetRelativePath ( "\\TYPELEVEN-HOME\backups\pls\test.wpl" ; "\\TYPELEVEN-HOME\backups\pls\New folder\LogMeIn.msi" ) = "New folder\LogMeIn.msi"

GetRelativePath ( "\\TYPELEVEN-HOME\backups\pls\test.wpl" ; "\\TYPELEVEN-HOME\backups\DP0304P.exe" ) = "..\DP0304P.exe"

GetRelativePath ( "\\TYPELEVEN-HOME\backups\pls\test.wpl" ; "\\TYPELEVEN-HOME\Done\Software\cr-1nb03.zip" ) = "..\..\Done\Software\cr-1nb03.zip"

Anyone seen a function like this?

Link to comment
Share on other sites

Anyone seen a function like this?

I haven't, but it shouldn't be very difficult to construct. Roughly:

Compare the first directory of both paths; if they are the same, remove the first directory from both and call the function again. Otherwise count the remaining directories in the source path, and tack as many "../" in front of the target path.

Link to comment
Share on other sites

I think I figured it out. Hows this one look? Can you think of anything that would mess this one up?

GetReletivePath ( from ; to )

Case(



IsEmpty($i) ; Let($i = 1 ; GetReletivePath(from ; to));



Left(from;$i) = Left(to;$i) ;  Let($i = $i + 1 ; GetReletivePath(from ; to));



Let([

fromRemainder = Middle(from;$i;Length(from));

toRemainder =  Middle( to;$i - 1 ;Length(to) );

posOfFirsttSlash =  Position (   toRemainder ; "\\" ; 1; 1);

toRemainder =  Middle( to ; $i + posOfFirsttSlash - 1  ; Length(to) );

slashes = Substitute (Filter (  fromRemainder; "\\")  ; "\\"; "..\\" );

$i = ""

];

slashes & toRemainder

)

)

Also is there a good explanation of how local variables behave in custom functions?

Link to comment
Share on other sites

I think that checking identity character-by-character is inefficient, compared to checking directory-by-directory.

Moreover, it creates a problem when two corresponding directories start with the same character/s. For example, given:

from = "\\NET\dir\ab\source.txt"

to = "\\NET\dir\abc\target.txt"

your function returns:

"..\target.txt"

while IMHO the correct result is:

"..\abc\target.txt"

I would do it this way:

http://www.briandunning.com/cf/1251

Link to comment
Share on other sites

This topic is 4889 days old. Please don't post here. Open a new topic instead.

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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.