Getting relative HTML path

If you are writing a script to deal with HTML, you may need a function to find out the relative path to a folder. The following function does this. It takes source and destination folders (in Windows notation - using "\" as folder name delimiter) and converts the resulting relative path from the source to destination to HTML standard (using ".." to indicate parent folder and "/" to delimit folder names).

FUNCTION GetRelativePath(Source$,Destination$) AS STRING
	s$=Source
	IF LEFT(s$,1)<>"\" AND MID(s$,2,1)<>":" THEN s$=GetCurrFolder()+"\"+Source
	IF LEFT(s$,1)="\" THEN s$=LEFT(GetCurrFolder(),2)+s$
	Src$=LTRIM(RTRIM(MID(s,3))) ' Remove Drive Letter
	s$=Destination
	IF LEFT(s$,1)<>"\" AND MID(s$,2,1)<>":" THEN s$=GetCurrFolder()+"\"+Destination
	IF LEFT(s$,1)="\" THEN s$=LEFT(GetCurrFolder(),2)+s$
	Dest$=LTRIM(RTRIM(MID(s,3)))
	IF RIGHT(Src,1)<>"\" THEN Src=Src+"\"
	IF RIGHT(Dest,1)<>"\" THEN Dest=Dest+"\"
	i1&=0
	i2&=0
	DO
		p1&=i1
		p2&=i1
		i1&=InStr(Src,"\",p1+1)
		i2&=InStr(Dest,"\",p1+1)
	LOOP WHILE i1<>0 AND i2<>0 AND i1=i2 AND LEFT(Src,i1)=LEFT(Dest,i2)
	Res$=""
	WHILE i1<>0
		i1=InStr(Src,"\",i1+1)
		Res=Res+"../"
	WEND
	Res=Res+MID(Dest,p2+1)
	i1=InStr(Res,"\")
	WHILE i1<>0
		MID(Res,i1,1)="/"
		i1=InStr(Res,"\",i1+1)
	WEND
	IF Res<>"" AND RIGHT(Res,1)<>"/" THEN Res=Res+"/"
	i1=InStr(Res," ")
	WHILE i1<>0
		Res=LEFT(Res,i1-1)+"%20"+MID(Res,i1+1)
		i1=InStr(Res," ",i1+1)
	WEND
	GetRelativePath=Res
END FUNCTION

Here are some examples of results produced by this function:

Source Folder Destination Folder Relative Path
C:\TEMP\ C:\TEMP\Images\ Images/
C:\TEMP\Images\ C:\TEMP\ ../
website\html\pages website\images\clipart ../../images/clipart/

Note that this function is case sensitive.


[ CorelSCRIPT Tips&Tricks | Learning CorelSCRIPT | Oberon Home Page ]

Copyright 2000 by Alex Vakulenko. All rights reserved.
This page was last revised on 01/13/00.