Are there standard functions to perform absolute <--> relative path conversion in Delphi?
For example:
'C:\Projects\Project1\'
'..\Shared\somefile.pas'
'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\')
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.
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).
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.
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.
To convert to the absolute you have :
ExpandFileName
To have the relative path you have :
ExtractRelativePath
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With