Relative path

BlitzMax Forums/BlitzMax Programming/Relative path

Oddball(Posted 2006) [#1]
Hi all, is there a way to extract a files path relative to another file. For example,

source - "C:/folder1/folder2/file1.exe"
dest - "C:/folder1/folder3/file2.dat"
result - "../folder3/file2.dat"

Any help would be appreciated. Thank you.


Azathoth(Posted 2006) [#2]
Don't really understand the question.
Something like ChangeDir("C:/folder1/")?


Scott Shaver(Posted 2006) [#3]
write a function that compares the two strings from the start to the end. When they don't match anymore build the correct relative path and return it. Use the string methods to look for / and \, somthing like this

Local index:Int = filename.FindLast("/")
Local path:String = ""
If index=-1 Then index = filename.FindLast("\")
If index<>-1 Then path=filename[..index]


Oddball(Posted 2006) [#4]
I was hoping to avoid doing this myself. Anyway I've had a go. If there's anyone who is more knowledgable about these things than me that could check it over and tell me if I'm doing this right I'd much appreciate it.
Strict

Local source:String=RequestFile("source")
Local destination:String=RequestFile("destination")

Print ExtractRelativePath(source,destination)

Function ExtractRelativePath:String( src:String, dest:String )
	
	src=ExtractDir(RealPath(src))
	dest=RealPath(dest)
	
	Local count:Int=0
	While src<>Left(dest,Len(src))
		count:+1
		Local i:Int=src.FindLast("/")
		src=src[..i]
	Wend
	
	dest=dest.Replace(src,"")
	dest=dest[1..]
	For Local i:Int=1 To count
		dest="../"+dest
	Next
	
	Return dest
	
End Function
I have no idea if it's cross platform, but it seems to work on windows. Surely someone has already done this? Thanks.