PathCombine & ExtractRelativePath Example in Delphi

// PathCombine & ExtractRelativePath Example

{
Question:

Could anyone point me to any function that can receive
an HTML style relative path as its input and return a full path as its
output based on the exe’s current location, e.g

Input: ‘..\..\test2.dat’
Output: C:\folder1\test2.dat

(assuming that the executable is running from
C:\folder1\folder2\folder3\Project1.exe)

}

{

Answer:

PathCombine() Concatenates two strings that represent properly
formed paths into one path, as well as any relative path pieces.
}

function PathCombine(lpszDest: PChar; const lpszDir, lpszFile: PChar):
PChar; stdcall; external ‘shlwapi.dll’ name ‘PathCombineA’;
function PathCombineA(lpszDest: PAnsiChar; const lpszDir, lpszFile:
PAnsiChar): PAnsiChar; stdcall; external ‘shlwapi.dll’;
function PathCombineW(lpszDest: PWideChar; const lpszDir, lpszFile:
PWideChar): PWideChar; stdcall; external ‘shlwapi.dll’;

{
Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later);
Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later)
}

procedure TForm1.FormCreate(Sender: TObject);
var
dest: string;
BaseFile, RelativePath: String;
begin
BaseFile := ‘C:\folder1\folder2\folder3\’;
RelativePath := ‘..\..\test2.dat’;
SetLength(dest, MAX_PATH);
PathCombine(@dest[1], PChar(ExtractFilePath(BaseFile)), PChar(RelativePath));
SetLength(dest, StrLen(@Dest[1]));
ShowMessage(dest);
end;

{
The ExtractRelativePath function takes a base directory but the
ExpandFilename function does the expansion based on the current
directory not a given base path.
}

RelativePath := ExtractRelativePath(ExtractFilePath(BaseFile), TargetFile);