0

I often use ExtractRelativePath to get the relative path between two path. But i cannot see any function opposite to it. This is an example from freepascal.org:

Uses sysutils;

Procedure Testit (FromDir,ToDir : String);

begin
  Write ('From "',FromDir,'" to "',ToDir,'" via "');
  Writeln (ExtractRelativePath(FromDir,ToDir),'"');
end;

Begin
 Testit ('/pp/src/compiler','/pp/bin/win32/ppc386');
 Testit ('/pp/bin/win32/ppc386','/pp/src/compiler');
 Testit ('e:/pp/bin/win32/ppc386','d:/pp/src/compiler');
 Testit ('e:\pp\bin\win32\ppc386','d:\pp\src\compiler');
End.

Output of this program

From "/pp/src/compiler" to "/pp/bin/win32/ppc386" via "../bin/win32/ppc386"
From "/pp/bin/win32/ppc386" to "/pp/src/compiler" via "../../src/compiler"
From "e:/pp/bin/win32/ppc386" to "d:/pp/src/compiler" via "../../src/compiler"
From "e:\pp\bin\win32\ppc386" to "d:\pp\src\compiler" via "../../src/compiler"

I need a function F to perform reverse action of ExtractRelativePath, for example:

F('/pp/src/compiler', '../bin/win32/ppc386') return '/pp/bin/win32/ppc386'.

Do you know any function like this? Thank you in advance.

kien_coi_1997
  • 129
  • 2
  • 14
  • 2
    possible duplicate of [Conversion between absolute and relative paths in Delphi](http://stackoverflow.com/questions/5329472/conversion-between-absolute-and-relative-paths-in-delphi) – GolezTrol Jan 17 '14 at 07:52
  • @GolezTrol that is not about expanding, that si about merging full with relative. – Arioch 'The Jan 17 '14 at 07:58
  • http://www.delphipages.com/forum/showthread.php?t=173795 – Arioch 'The Jan 17 '14 at 08:48
  • http://www.freepascal.org/docs-html/rtl/sysutils/concatpaths.html – Arioch 'The Jan 17 '14 at 09:02
  • @DavidHeffernan : I'm sorry, this is only my mistake, I've edit the content from ../bin/win32/ppc386 to /pp/bin/win32/ppc386. – kien_coi_1997 Jan 17 '14 at 09:04
  • OK. I've added another correction. – David Heffernan Jan 17 '14 at 09:05
  • Thank you for your time. But your correction is not right. The first parameter is '/pp/src/compiler' but not '/pp/src/compiler/', so the second parameter must be '../bin/win32/ppc386' not '../../bin/win32/ppc386'. I'm reading the answers. – kien_coi_1997 Jan 17 '14 at 09:23
  • @GolezTrol : I think that the answers in [this link](http://stackoverflow.com/questions/5329472/conversion-between-absolute-and-relative-paths-in-delphi) is too complex, I've read that link before ask this question. And I need a simplier solution. – kien_coi_1997 Jan 17 '14 at 09:45

1 Answers1

4

Yes, sure. http://docwiki.embarcadero.com/Libraries/XE5/en/System.IOUtils.TPath.Combine

System.IOUtils.TPath.Combine

  class function Combine(const Path1, Path2: string): string; inline; static;

Description

Combines two paths strings.

Call Combine to obtain a new combined path from two distinct paths. If the second path is absolute, Combine returns it directly; otherwise Combine returns the first path concatenated with the second one.


Above was written when the question was tagged by

Now, for FPC simple scan through SysUtils sources lands you onto

  • c:\codetyphon\fpcsrc\rtl\objpas\sysutils\finah.inc

Which has

function ConcatPaths(const Paths: array of String): String;

Which is documented at

ConcatPaths

Concatenate an array of paths to form a single path

Declaration

Source position: finah.inc line 42 function ConcatPaths( const Paths: array of ):;

Description

ConcatPaths will concatenate the different path components in Paths to a single path. It will insert directory separators between the various components of the path as needed. No directory separators will be added to the beginning or the end of the path, and none will be taken away.

Example

program ex96;

{ This program demonstrates the Concatpaths function }

uses sysutils;

begin
  // will write /this/path/more/levels/
  Writeln(ConcatPaths(['/this/','path','more/levels/']));
  // will write this/path/more/levels/
  Writeln(ConcatPaths(['this/','path','more/levels/']));
  // will write this/path/more/levels
  Writeln(ConcatPaths(['this/','path','more/levels']));
end.
Arioch 'The
  • 15,799
  • 35
  • 62