Sign in Join
FPDoc Onlineexperiment

TryCreateRelativePath function

In unit LazFileUtils

Attempts to create a path in Dest relative to the path in Source.

Declaration

pascal
function TryCreateRelativePath(const Dest: string; 
 const Source: string; 
 UsePointDirectory: Boolean; 
 AlwaysRequireSharedBaseFolder: Boolean; 
 out RelPath: string): Boolean

Description

Returns True if it is possible to create a relative path from Source to Dest. The function must be thread safe, so no expansion of filenames is done since this is not thread-safe (at least on Windows platform).

  • Both Dest and Source must be either absolute filenames or relative file names.
  • Dest and Source cannot contain '..' since no expansion is done by design.
  • Dest and Source must be on same drive or UNC path (Windows).
  • If both Dest and Source are relative they must at least share their base directory.
  • Double PathDelims are ignored (unless they are part of the UNC convention).
  • If UsePointDirectory is True and RelPath is an empty string (''), RelPath is set to the current directory ('.').
  • If AlwaysRequireSharedBaseFolder is False then Absolute filenames need not share a base folder.
  • If the function succeeds, RelPath contains the relative path from Source to Dest. A PathDelimiter is not appended to the end of RelPath.
TryCreateRelativePath does not physically create any folders need for the derived relative path to Dest.

Examples:

- Dest = /foo/bar Source = /foo Result = True RelPath = bar
- Dest = /foo///bar Source = /foo// Result = True RelPath = bar
- Dest = /foo Source = /foo/bar Result = True RelPath = ../
- Dest = /foo/bar Source = /bar Result = True RelPath = ../foo/bar
- Dest = foo/bar Source = foo/foo Result = True RelPath = ../bar
- Dest = foo/bar Source = bar/foo Result = False (no shared base directory)
- Dest = /foo Source = bar Result = False (mixed absolute and relative)
- Dest = c:foo Source = c:bar Result = False (no expanding)
- Dest = c:\foo Source = d:\bar Result is False (different drives)
- Dest = \foo Source = foo (Windows) Result is False (too ambiguous to guess what this should mean)
- Dest = /foo Source = /bar AlwaysRequireSharedBaseFolder = True Result = False
- Dest = /foo Source = /bar AlwaysRequireSharedBaseFolder = False Result = True RelPath = ../foo

An experiment, built from Lazarus main 2dfa93c66e of 10-22-2026. Not the official Lazarus documentation.