Mega Code Archive

 
Categories / Delphi / Examples
 

How to use PathCombine & ExtractRelativePath

Title: How to use PathCombine & ExtractRelativePath 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);