Skip to content

ExtractRelativePath

Extract a relative path from a filename, given a base directory.

Declaration

Source position: finah.inc line 33

  function ExtractRelativePath(const BaseName: UNICODESTRING; 
                              const DestName: UNICODESTRING)
                               : UNICODESTRING;
  function ExtractRelativePath(const BaseName: RAWBYTESTRING; 
                              const DestName: RAWBYTESTRING)
                               : RAWBYTESTRING;

Description

ExtractRelativePath constructs a relative path to go from BaseName to DestName. If DestName is on another drive (Not on Unix-like platforms) then the whole Destname is returned.

Note that directories must end on a path delimiter for this function to work correctly. If not, the last part is stripped and treated as a file name.

Errors

None.

See also

Name Description
ExtractFileDir Extract the drive and directory part of a filename.
ExtractFileDrive Extract the drive part from a filename.
ExtractFileExt Return the extension from a filename.
ExtractFileName Extract the filename part from a full path filename.
ExtractFilePath Extract the path from a filename.

Example

Program Example35;
{ This program demonstrates the ExtractRelativePath function }
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 ('/pp/bin/win32/','/pp/src/compiler/ppcx386/');
 Testit ('/pp/bin/win32/','/pp/src/compiler/ppcx386');
 Testit ('/pp/bin/win32','/pp/src/compiler/ppcx386');
 Testit ('e:/pp/bin/win32/ppc386/','d:/pp/src/compiler/');
 Testit ('e:\pp\bin\win32\ppc386/','d:\pp\src\compiler/');
 Testit ('C:\FPC\3.0.2\','C:\FPC\3.0.2\');
 Testit ('C:\FPC\3.0.2\','C:\FPC\3.0.4rc1\');
 Testit ('Q:\','Q:\FPC\3.0.4rc1\');
End.