December 12, 201015 yr 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?
December 12, 201015 yr 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.
December 13, 201015 yr Author 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?
December 13, 201015 yr 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
Create an account or sign in to comment