converting paths to relative or absolute

Share your advanced PureBasic knowledge/code with the community.
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

converting paths to relative or absolute

Post by Klonk »

Code updated for 5.20+

I needed this in a project. Maybe someone else find it useful too:

Code: Select all

;{ ===================================================================
;|  ConvertToRelativePath(TargetPath$,ActualPath$)
;|    Returns the path of TargetPath$ in relation to ActualPath$,
;|    i.e creates a relative path to TargetPath$ with ActualPath$ as
;|    starting point
;} ===================================================================
Procedure.s ConvertToRelativePath(TargetPath$,ActualPath$)
  ;***** TargetPath$ = Path to be converted (relative to ActualPath$)
  If Left(TargetPath$,3)<>Left(ActualPath$,3)
    ;Target is located on a different drive -> no relative path possible
    RelativePath$ = TargetPath$
  ElseIf TargetPath$<>ActualPath$
    ;remove identical parts of both paths
    backslashposition = 0
    Repeat
      TargetPath$ = Right(TargetPath$,Len(TargetPath$)-backslashposition)
      ActualPath$ = Right(ActualPath$,Len(ActualPath$)-backslashposition)
      backslashposition = FindString(TargetPath$,"\",1)
    Until Left(TargetPath$,backslashposition) <> Left(ActualPath$,FindString(ActualPath$,"\",1))
    ;get number of remaining backslashes
    backslashcount = CountString(ActualPath$,"\")
    RelativePath$ = TargetPath$
    ;add relative paths
    For i=0 To backslashcount
      RelativePath$ = "..\" + RelativePath$
    Next i
  Else
    ;Target is located in the same directory
    RelativePath$ =  ".\"
  EndIf
  ProcedureReturn RelativePath$
EndProcedure

;{ ===================================================================
;|  ConvertToAbsolutePath(TargetPath$,ActualPath$)
;|    Returns the abolute path (with driveletter etc.) to the relative
;|    TargetPath$ where ActualPath$ is the starting point
;} ===================================================================
Procedure.s ConvertToAbsolutePath(TargetPath$,ActualPath$)
  ;***** TargetPath$ = realtive to be converted in relation to ActualPath$)
  If TargetPath$ = ".\" ;same as ActualPath
    AbsolutePath$ = ActualPath$
  ElseIf (Mid(TargetPath$, 2, 1) = ":") Or (Left(TargetPath$,2) = "\\") Or (Left(TargetPath$,1) = "%")
    ;Targetpath has a drive letter or is network drive or starts with environment variable
      AbsolutePath$ = TargetPath$
  Else ;some other relative path
    If Right(ActualPath$, 1) = "\"
      ActualPath$ = Left(ActualPath$,Len(ActualPath$)-1) ;remove last backslash
    EndIf
    While Left(TargetPath$,3) = "..\"
      TargetPath$ = Right(TargetPath$, Len(TargetPath$)-3)
      lastbackslashposition = 1
      While FindString(ActualPath$,"\",lastbackslashposition+1)<>0
        lastbackslashposition = FindString(ActualPath$,"\",lastbackslashposition+1)
      Wend
      ActualPath$ = Left(ActualPath$,lastbackslashposition-1) ;remove unecessary part
    Wend
    AbsolutePath$ = ActualPath$ + "\" + TargetPath$
  EndIf
  ProcedureReturn AbsolutePath$
EndProcedure
Last edited by Klonk on Wed Apr 05, 2006 8:28 am, edited 1 time in total.
Bye Karl
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Cool :D. Thanks.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: converting paths to relative or absolute

Post by PB »

Got a usage example?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

Post by Klonk »

Usage example (some imaginary directories):
Situation: you are in the directory C:\program files\purebasic and want a relative path to C:\Windows\system

Code: Select all

path$ = ConvertToRelativePath("C:\windows\system","C:\program files\purebasic")
   ; path$ will be "..\..\windows\system"
path$ = ConvertToRelativePath("C:\program files\purebasic","C:\program files\purebasic")
   ; path$ will be ".\"
path$ = ConvertToRelativePath("D:\temp","C:\program files\purebasic")
   ; path$ will be "D:\temp"
Situation: you are in the directory C:\program files\purebasic and want the complete path from given relative paths (maybe created by "ConvertToRelativePath"):

Code: Select all

path$ = ConvertToAbsolutePath("..\acrobat","C:\program files\purebasic")
   ; path$ will be "C:\program files\acrobat"
path$ = ConvertToAbsolutePath(".\","C:\program files\purebasic")
   ; path$ will be "C:\program files\purebasic"
path$ = ConvertToAbsolutePath("","C:\program files\purebasic")
   ; path$ will be ""
path$ = ConvertToAbsolutePath("D:\temp","C:\program files\purebasic")
   ; path$ will be "D:\temp"
Hope this helps.

BTW: changed the above code a little bit to also take care of paths starting with environment variables
Bye Karl
Post Reply