[Gambas-user] Concatenate two path strings

T Lee Davidson t.lee.davidson at gmail.com
Sun Oct 1 06:31:05 CEST 2023


On 9/30/23 23:23, BB wrote:
> I have two strings representing a "base" path and a "relative" path, so that, for instance
>      basepath = "/home/me/ahab/bhab" ' or sometimes "/home/me/ahab/bhab/"
>      relpath = "../../img/etc" ' or sometimes "img/etc" or "/img/etc" or ./img/etc" or anything else that the user has 
> constructed the relative path as
> 
> I know I can get a full path just by concatenting using &/ i.e.
>      fullpath = basepath &/ relpath
> 
> which seems to work but results often in a crazy looking path. Short of manually parsing the basepath and relpath variables into 
> some sort of standard using a lot of ifs, is there a simple Gambas way of getting the proper path string? I can't find one.
> 
> Or even a external call to something that could resolve this. Strangely in all my years of using Gambas I have never had this 
> need before.
> 
> tia
> 
> bruce

You simplified the problem description again, right? ;-)

Perhaps 'realpath' might help you normalize the relative path at the point of input:
[code]
Public Sub main()

   Dim sRealPath As String
   Dim sUserInput As String = "../../img/etc"

   Exec ["realpath", sUserInput] To sRealPath With Error
   If InStr(sRealPath, "No such file or directory") Then
     Print "Bad user. Bad, bad user!"
     Quit
   Endif
   Print sRealPath

End
[/code]

BTW, the two basepaths you mentioned appear to be the same.
And, "/img/etc" is not a relative path. So, if a user might enter an absolute path for a relative path, you might want to check 
for that and strip it off first thing.


-- 
Lee



More information about the User mailing list