Resolving relative paths (all OS)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Resolving relative paths (all OS)

Post by Thorsten1867 »

Code: Select all

Procedure.s GetAbsolutePath(Path.s, File.s)
  Define.i i, PS
  Define.s PathPart$, Path$
  
  PathPart$ = GetPathPart(File)
  
  If PathPart$
    
    If CountString(PathPart$, ":" + #PS$) = 1 Or Left(PathPart$, 2) = #PS$ + #PS$ ;{ Absolute path name
      
      ProcedureReturn File
      ;}
    Else                                                                          ;{ Relative path name
      
      If Left(PathPart$, 3) = ".." + #PS$    ;{ A relative path to a file in a directory that is a peer of the current directory

        Path$ = ""
        Path  = ReplaceString(Path, #PS$ + #PS$, "|" + #PS$)
        
        PS = CountString(Path, #PS$)
        If PS > 1
          For i=1 To PS - 1
            Path$ + StringField(Path, i, #PS$) + #PS$
          Next
          ProcedureReturn ReplaceString(Path$, "|", #PS$) + Mid(File, 4)
        Else
          ProcedureReturn ReplaceString(Path,  "|", #PS$) + Mid(File, 4)
        EndIf  
        ;}
      ElseIf Left(PathPart$, 2) = "." + #PS$ ;{ A relative path to a file in the current directory    
        ProcedureReturn Path + Mid(File, 3)
        ;}
      ElseIf Left(PathPart$, 1) = #PS$       ;{ An absolute path from the root of the current drive

        Path  = ReplaceString(Path, #PS$ + #PS$, "|" + #PS$)
        Path$ = StringField(Path, 1, #PS$) + #PS$
        
        ProcedureReturn ReplaceString(Path$, "|", #PS$) + Mid(File, 2)
        ;}
      ElseIf Mid(PathPart$, 2, 1) = ":"      ;{ A relative path from the current directory of the drive
        
        Path$ = Left(PathPart$, 2) + Mid(Path, 3)
        
        ProcedureReturn  Path$ + Mid(File, 3)
        ;}  
      Else                                   ;{ A relative path to a file in a subdirectory of the current directory
        ProcedureReturn Path + File
        ;}
      EndIf
      ;}
    EndIf
    
  Else
    ProcedureReturn Path + File
  EndIf  
  
EndProcedure  

CurrentPath$ = "D:\Entwicklung\Source\Test\"
Debug "Current Path: " + CurrentPath$
Debug "=============================="

Debug "Absolute path name:"
Debug GetAbsolutePath(CurrentPath$, "C:\Test\Images\Test.png")
Debug ""
Debug "A relative path to a file in the current directory:"
Debug GetAbsolutePath(CurrentPath$, ".\Images\Test.png")
Debug ""
Debug "A relative path to a file in a directory that is a peer of the current directory:"
Debug GetAbsolutePath(CurrentPath$, "..\Images\Test.png")
Debug ""
Debug "An absolute path from the root of the current drive:"
Debug GetAbsolutePath(CurrentPath$, "\Images\Test.png")
Debug ""
Debug "A relative path from the current directory of the drive:"
Debug GetAbsolutePath(CurrentPath$, "C:Images\Test.png")
Debug ""
Debug "A relative path to a file in a subdirectory of the current directory:"
Debug GetAbsolutePath(CurrentPath$, "Images\Test.png")
Debug GetAbsolutePath(CurrentPath$, "Test.png")
Last edited by Thorsten1867 on Fri Apr 03, 2020 2:03 pm, edited 1 time in total.
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Resolving relative paths

Post by Josh »

If it's Windows only, you should have a look to 'GetFullPathName_()'
sorry for my bad english
Taz
User
User
Posts: 75
Joined: Sat Jan 20, 2018 5:28 pm
Location: Germany

Re: Resolving relative paths (all OS)

Post by Taz »

there is a mistake.

Code: Select all

Debug "Test: " + GetAbsolutePath(CurrentPath$, "..\..\Test.png")
result:

Code: Select all

Test: D:\Entwicklung\Source\..\Test.png
correct would be:

Code: Select all

D:\Entwicklung\Test.png
Post Reply