whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

50 Visitors Online


 
...use PathCombine & ExtractRelativePath?
Autor: Ken Revak
[ Print tip ]  

Tip Rating (7):  
     


{
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; stdcallexternal 'shlwapi.dll' name 'PathCombineA';
function PathCombineA(lpszDest: PAnsiChar; const lpszDir, lpszFile:
PAnsiChar): PAnsiChar; stdcallexternal 'shlwapi.dll';
function PathCombineW(lpszDest: PWideChar; const lpszDir, lpszFile:
PWideChar): PWideChar; stdcallexternal '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);


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners