Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion between absolute and relative paths in Delphi

Tags:

path

delphi

Are there standard functions to perform absolute <--> relative path conversion in Delphi?

For example:

  • 'Base' path is 'C:\Projects\Project1\'
  • Relative path is '..\Shared\somefile.pas'
  • Absolute path is 'C:\Projects\Shared\somefile.pas'

I am looking for something like this:

function AbsToRel(const AbsPath, BasePath: string): string; // '..\Shared\somefile.pas' = //   AbsToRel('C:\Projects\Shared\somefile.pas', 'C:\Projects\Project1\')   function RelToAbs(const RelPath, BasePath: string): string; // 'C:\Projects\Shared\somefile.pas' = //   RelToAbs('..\Shared\somefile.pas', 'C:\Projects\Project1\')   
like image 550
kludg Avatar asked Mar 16 '11 17:03

kludg


People also ask

How do you convert an absolute path to a relative path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

What are the differences between absolute paths and relative paths?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

What is the relative and absolute path explain with an example?

For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string. A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path.

What are absolute and relative paths for directories and files?

relative path name Traces the path from the current directory through its parent or its subdirectories and files. An absolute path name represents the complete name of a directory or file from the /(root) directory downward.


2 Answers

To convert to the absolute you have :

ExpandFileName

To have the relative path you have :

ExtractRelativePath

like image 128
philnext Avatar answered Oct 11 '22 15:10

philnext


I would use PathRelativePathTo as the first function and PathCanonicalize as the second. In the latter case, as argument you pass the string sum of the base path and the relative path.

function PathRelativePathTo(pszPath: PChar; pszFrom: PChar; dwAttrFrom: DWORD;   pszTo: PChar; dwAtrTo: DWORD): LongBool; stdcall; external 'shlwapi.dll' name 'PathRelativePathToW';  function AbsToRel(const AbsPath, BasePath: string): string; var   Path: array[0..MAX_PATH-1] of char; begin   PathRelativePathTo(@Path[0], PChar(BasePath), FILE_ATTRIBUTE_DIRECTORY, PChar(AbsPath), 0);   result := Path; end;  function PathCanonicalize(lpszDst: PChar; lpszSrc: PChar): LongBool; stdcall;   external 'shlwapi.dll' name 'PathCanonicalizeW';  function RelToAbs(const RelPath, BasePath: string): string; var   Dst: array[0..MAX_PATH-1] of char; begin   PathCanonicalize(@Dst[0], PChar(IncludeTrailingBackslash(BasePath) + RelPath));   result := Dst; end;   procedure TForm4.FormCreate(Sender: TObject); begin   ShowMessage(AbsToRel('C:\Users\Andreas Rejbrand\Desktop\file.txt', 'C:\Users\Andreas Rejbrand\Pictures'));   ShowMessage(RelToAbs('..\Videos\movie.wma', 'C:\Users\Andreas Rejbrand\Desktop')); end; 

Of course, if you use a non-Unicode version of Delphi (that is, <= Delphi 2007), you need to use the Ansi functions (*A) instead of the Unicode functions (*W).

like image 44
Andreas Rejbrand Avatar answered Oct 11 '22 13:10

Andreas Rejbrand